Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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 :
18 : #ifdef HAVE_CONFIG_H
19 : #include "config.h"
20 : #endif
21 :
22 : #include "manager.h"
23 :
24 : #include "logger.h"
25 : #include "account_schema.h"
26 :
27 : #include "fileutils.h"
28 : #include "gittransport.h"
29 : #include "jami.h"
30 : #include "media_attribute.h"
31 : #include "media/system_codec_container.h"
32 : #include "account.h"
33 : #include "string_utils.h"
34 : #include "jamidht/jamiaccount.h"
35 : #include "account.h"
36 : #include <opendht/rng.h>
37 :
38 : #include "call_factory.h"
39 :
40 : #include "sip/sipvoiplink.h"
41 : #include "sip/sipaccount_config.h"
42 :
43 : #include "im/instant_messaging.h"
44 :
45 : #include "config/yamlparser.h"
46 :
47 : #if HAVE_ALSA
48 : #include "audio/alsa/alsalayer.h"
49 : #endif
50 :
51 : #include "audio/sound/dtmf.h"
52 : #include "audio/ringbufferpool.h"
53 :
54 : #ifdef ENABLE_PLUGIN
55 : #include "plugin/jamipluginmanager.h"
56 : #include "plugin/streamdata.h"
57 : #endif
58 :
59 : #include "client/videomanager.h"
60 :
61 : #include "conference.h"
62 :
63 : #include "client/jami_signal.h"
64 : #include "jami/call_const.h"
65 :
66 : #include "libav_utils.h"
67 : #ifdef ENABLE_VIDEO
68 : #include "video/sinkclient.h"
69 : #include "video/video_base.h"
70 : #include "media/video/video_mixer.h"
71 : #endif
72 : #include "audio/tonecontrol.h"
73 :
74 : #include <dhtnet/ice_transport_factory.h>
75 : #include <dhtnet/ice_transport.h>
76 : #include <dhtnet/upnp/upnp_context.h>
77 :
78 : #include <libavutil/ffversion.h>
79 :
80 : #include <opendht/thread_pool.h>
81 :
82 : #include <asio/io_context.hpp>
83 : #include <asio/executor_work_guard.hpp>
84 :
85 : #include <git2.h>
86 :
87 : #ifndef WIN32
88 : #include <sys/time.h>
89 : #include <sys/resource.h>
90 : #endif
91 :
92 : #ifdef TARGET_OS_IOS
93 : #include <CoreFoundation/CoreFoundation.h>
94 : #endif
95 :
96 : #include <cerrno>
97 : #include <ctime>
98 : #include <cstdlib>
99 : #include <iostream>
100 : #include <fstream>
101 : #include <algorithm>
102 : #include <memory>
103 : #include <mutex>
104 : #include <list>
105 : #include <random>
106 :
107 : #ifndef JAMI_DATADIR
108 : #error "Define the JAMI_DATADIR macro as the data installation prefix of the package"
109 : #endif
110 :
111 : namespace jami {
112 :
113 : /** To store uniquely a list of Call ids */
114 : using CallIDSet = std::set<std::string>;
115 :
116 : static constexpr const char* PACKAGE_OLD = "ring";
117 :
118 : std::atomic_bool Manager::initialized = {false};
119 :
120 : #if TARGET_OS_IOS
121 : bool Manager::isIOSExtension = {false};
122 : #endif
123 :
124 : bool Manager::syncOnRegister = {true};
125 :
126 : bool Manager::autoLoad = {true};
127 :
128 : static void
129 32 : copy_over(const std::filesystem::path& srcPath, const std::filesystem::path& destPath)
130 : {
131 32 : std::ifstream src(srcPath);
132 32 : std::ofstream dest(destPath);
133 32 : dest << src.rdbuf();
134 32 : src.close();
135 32 : dest.close();
136 32 : }
137 :
138 : // Creates a backup of the file at "path" with a .bak suffix appended
139 : static void
140 29 : make_backup(const std::filesystem::path& path)
141 : {
142 29 : auto backup_path = path;
143 29 : backup_path.replace_extension(".bak");
144 29 : copy_over(path, backup_path);
145 29 : }
146 :
147 : // Restore last backup of the configuration file
148 : static void
149 3 : restore_backup(const std::filesystem::path& path)
150 : {
151 3 : auto backup_path = path;
152 3 : backup_path.replace_extension(".bak");
153 3 : copy_over(backup_path, path);
154 3 : }
155 :
156 : static void
157 96 : check_rename(const std::filesystem::path& old_dir, const std::filesystem::path& new_dir)
158 : {
159 96 : if (old_dir == new_dir or not std::filesystem::is_directory(old_dir))
160 64 : return;
161 :
162 32 : std::error_code ec;
163 32 : if (not std::filesystem::is_directory(new_dir)) {
164 0 : JAMI_WARNING("Migrating {} to {}", old_dir, new_dir);
165 0 : std::filesystem::rename(old_dir, new_dir, ec);
166 0 : if (ec)
167 0 : JAMI_ERROR("Failed to rename {} to {}: {}", old_dir, new_dir, ec.message());
168 : } else {
169 32 : for (const auto& file_iterator : std::filesystem::directory_iterator(old_dir, ec)) {
170 0 : const auto& file_path = file_iterator.path();
171 0 : auto new_path = new_dir / file_path.filename();
172 0 : if (file_iterator.is_directory() and std::filesystem::is_directory(new_path)) {
173 0 : check_rename(file_path, new_path);
174 : } else {
175 0 : JAMI_WARNING("Migrating {} to {}", old_dir, new_path);
176 0 : std::filesystem::rename(file_path, new_path, ec);
177 0 : if (ec)
178 0 : JAMI_ERROR("Failed to rename {} to {}: {}", file_path, new_path, ec.message());
179 : }
180 0 : }
181 32 : std::filesystem::remove_all(old_dir, ec);
182 : }
183 : }
184 :
185 : /**
186 : * Set OpenDHT's log level based on the JAMI_LOG_DHT environment variable.
187 : * JAMI_LOG_DHT = 0 minimum logging (=disable)
188 : * JAMI_LOG_DHT = 1 logging enabled
189 : */
190 : static unsigned
191 32 : getDhtLogLevel()
192 : {
193 32 : if (auto* envvar = getenv("JAMI_LOG_DHT")) {
194 0 : return std::clamp(to_int<unsigned>(envvar, 0), 0u, 1u);
195 : }
196 32 : return 0;
197 : }
198 :
199 : static unsigned
200 32 : getDhtnetLogLevel()
201 : {
202 32 : if (auto* envvar = getenv("JAMI_LOG_DHTNET")) {
203 0 : return std::clamp(to_int<unsigned>(envvar, 0), 0u, 1u);
204 : }
205 32 : return 0;
206 : }
207 :
208 : /**
209 : * Set pjsip's log level based on the JAMI_LOG_SIP environment variable.
210 : * JAMI_LOG_SIP = 0 minimum logging
211 : * JAMI_LOG_SIP = 6 maximum logging
212 : */
213 : static void
214 32 : setSipLogLevel()
215 : {
216 32 : int level = 0;
217 32 : if (auto* envvar = getenv("JAMI_LOG_SIP")) {
218 0 : level = std::clamp(to_int<int>(envvar, 0), 0, 6);
219 : }
220 :
221 32 : pj_log_set_level(level);
222 32 : pj_log_set_log_func([](int level, const char* data, int len) {
223 0 : auto msg = std::string_view(data, len);
224 0 : if (level < 2)
225 0 : JAMI_XERR("{}", msg);
226 0 : else if (level < 4)
227 0 : JAMI_XWARN("{}", msg);
228 : else
229 0 : JAMI_XDBG("{}", msg);
230 0 : });
231 32 : }
232 :
233 : /**
234 : * Set gnutls's log level based on the JAMI_LOG_TLS environment variable.
235 : * JAMI_LOG_TLS = 0 minimum logging (default)
236 : * JAMI_LOG_TLS = 9 maximum logging
237 : */
238 : static void
239 32 : setGnuTlsLogLevel()
240 : {
241 32 : int level = 0;
242 32 : if (auto* envvar = getenv("JAMI_LOG_TLS")) {
243 0 : level = to_int<int>(envvar, 0);
244 0 : level = std::clamp(level, 0, 9);
245 : }
246 :
247 32 : gnutls_global_set_log_level(level);
248 41 : gnutls_global_set_log_function([](int level, const char* msg) { JAMI_XDBG("[{:d}]GnuTLS: {:s}", level, msg); });
249 32 : }
250 :
251 : //==============================================================================
252 :
253 : struct Manager::ManagerPimpl
254 : {
255 : explicit ManagerPimpl(Manager& base);
256 :
257 : bool parseConfiguration();
258 :
259 : /*
260 : * Play one tone
261 : * @return false if the driver is uninitialize
262 : */
263 : void playATone(Tone::ToneId toneId);
264 :
265 : int getCurrentDeviceIndex(AudioDeviceType type);
266 :
267 : /**
268 : * Process remaining participant given a conference and the current call ID.
269 : * Mainly called when a participant is detached or call ended (hang up).
270 : * @param current call id
271 : * @param conference pointer
272 : */
273 : void processRemainingParticipants(Conference& conf);
274 :
275 : /**
276 : * Create config directory in home user and return configuration file path
277 : */
278 : std::filesystem::path retrieveConfigPath() const;
279 :
280 : void unsetCurrentCall();
281 :
282 : void switchCall(const std::string& id);
283 :
284 : /**
285 : * Add incoming callid to the waiting list
286 : * @param id std::string to add
287 : */
288 : void addWaitingCall(const std::string& id);
289 :
290 : /**
291 : * Remove incoming callid to the waiting list
292 : * @param id std::string to remove
293 : */
294 : void removeWaitingCall(const std::string& id);
295 :
296 : void loadAccount(const YAML::Node& item, int& errorCount);
297 : void cleanupAccountStorage(const std::string& accountId);
298 :
299 : void sendTextMessageToConference(const Conference& conf,
300 : const std::map<std::string, std::string>& messages,
301 : const std::string& from) const noexcept;
302 :
303 : void bindCallToConference(Call& call, Conference& conf);
304 :
305 : void addMainParticipant(Conference& conf);
306 :
307 : bool hangupConference(Conference& conf);
308 :
309 : template<class T>
310 : std::shared_ptr<T> findAccount(const std::function<bool(const std::shared_ptr<T>&)>&);
311 :
312 : void initAudioDriver();
313 :
314 : void processIncomingCall(const std::string& accountId, Call& incomCall);
315 : static void stripSipPrefix(Call& incomCall);
316 :
317 : Manager& base_; // pimpl back-pointer
318 :
319 : std::shared_ptr<asio::io_context> ioContext_;
320 : std::thread ioContextRunner_;
321 :
322 : std::shared_ptr<dhtnet::upnp::UPnPContext> upnpContext_;
323 :
324 : std::atomic_bool autoAnswer_ {false};
325 :
326 : /** Application wide tone controller */
327 : ToneControl toneCtrl_;
328 : std::unique_ptr<AudioDeviceGuard> toneDeviceGuard_;
329 :
330 : /** Current Call ID */
331 : std::string currentCall_;
332 :
333 : /** Protected current call access */
334 : std::mutex currentCallMutex_;
335 :
336 : /** Protected sinks access */
337 : std::mutex sinksMutex_;
338 :
339 : /** Audio layer */
340 : std::shared_ptr<AudioLayer> audiodriver_ {nullptr};
341 : std::array<std::atomic_uint, 3> audioStreamUsers_ {};
342 :
343 : /* Audio device users */
344 : std::mutex audioDeviceUsersMutex_ {};
345 : std::map<std::string, unsigned> audioDeviceUsers_ {};
346 :
347 : // Main thread
348 : std::unique_ptr<DTMF> dtmfKey_;
349 :
350 : /** Buffer to generate DTMF */
351 : std::shared_ptr<AudioFrame> dtmfBuf_;
352 :
353 : std::shared_ptr<asio::steady_timer> dtmfTimer_;
354 :
355 : // To handle volume control
356 : // short speakerVolume_;
357 : // short micVolume_;
358 : // End of sound variable
359 :
360 : /**
361 : * Mutex used to protect audio layer
362 : */
363 : std::mutex audioLayerMutex_;
364 :
365 : /**
366 : * Waiting Call Vectors
367 : */
368 : CallIDSet waitingCalls_;
369 :
370 : /**
371 : * Protect waiting call list, access by many VoIP/audio threads
372 : */
373 : std::mutex waitingCallsMutex_;
374 :
375 : /**
376 : * Path of the ConfigFile
377 : */
378 : std::filesystem::path path_;
379 :
380 : /**
381 : * Instance of the RingBufferPool for the whole application
382 : *
383 : * In order to send signal to other parts of the application, one must pass through the
384 : * RingBufferMananger. Audio instances must be registered into the RingBufferMananger and bound
385 : * together via the Manager.
386 : *
387 : */
388 : std::unique_ptr<RingBufferPool> ringbufferpool_;
389 :
390 : std::atomic_bool finished_ {false};
391 :
392 : /* ICE support */
393 : std::shared_ptr<dhtnet::IceTransportFactory> ice_tf_;
394 :
395 : /* Sink ID mapping */
396 : std::map<std::string, std::weak_ptr<video::SinkClient>> sinkMap_;
397 :
398 : std::unique_ptr<VideoManager> videoManager_;
399 :
400 : std::unique_ptr<SIPVoIPLink> sipLink_;
401 : #ifdef ENABLE_PLUGIN
402 : /* Jami Plugin Manager */
403 : std::unique_ptr<JamiPluginManager> jami_plugin_manager;
404 : #endif
405 :
406 : std::mutex gitTransportsMtx_ {};
407 : std::map<git_smart_subtransport*, std::unique_ptr<P2PSubTransport>> gitTransports_ {};
408 :
409 : std::shared_ptr<SystemCodecContainer> systemCodecContainer_;
410 : };
411 :
412 38 : Manager::ManagerPimpl::ManagerPimpl(Manager& base)
413 38 : : base_(base)
414 38 : , ioContext_(std::make_shared<asio::io_context>())
415 38 : , upnpContext_(std::make_shared<dhtnet::upnp::UPnPContext>(nullptr, Logger::dhtLogger()))
416 38 : , toneCtrl_(base.preferences)
417 38 : , dtmfBuf_(std::make_shared<AudioFrame>())
418 38 : , ringbufferpool_(new RingBufferPool)
419 : #ifdef ENABLE_VIDEO
420 190 : , videoManager_(nullptr)
421 : #endif
422 : {
423 38 : jami::libav_utils::av_init();
424 38 : }
425 :
426 : bool
427 35 : Manager::ManagerPimpl::parseConfiguration()
428 : {
429 35 : bool result = true;
430 :
431 : try {
432 35 : std::ifstream file(path_);
433 35 : YAML::Node parsedFile = YAML::Load(file);
434 35 : file.close();
435 35 : const int error_count = base_.loadAccountMap(parsedFile);
436 :
437 35 : if (error_count > 0) {
438 24 : JAMI_WARNING("[config] Error while parsing {}", path_);
439 6 : result = false;
440 : }
441 35 : } catch (const YAML::BadFile& e) {
442 0 : JAMI_WARNING("[config] Unable to open configuration file");
443 0 : result = false;
444 0 : }
445 :
446 35 : return result;
447 : }
448 :
449 : /**
450 : * Multi Thread
451 : */
452 : void
453 122 : Manager::ManagerPimpl::playATone(Tone::ToneId toneId)
454 : {
455 122 : if (not base_.voipPreferences.getPlayTones())
456 0 : return;
457 :
458 122 : std::lock_guard lock(audioLayerMutex_);
459 122 : if (not audiodriver_) {
460 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
461 0 : return;
462 : }
463 :
464 122 : auto oldGuard = std::move(toneDeviceGuard_);
465 122 : toneDeviceGuard_ = base_.startAudioStream(AudioDeviceType::PLAYBACK);
466 122 : audiodriver_->flushUrgent();
467 122 : toneCtrl_.play(toneId);
468 122 : }
469 :
470 : int
471 0 : Manager::ManagerPimpl::getCurrentDeviceIndex(AudioDeviceType type)
472 : {
473 0 : if (not audiodriver_)
474 0 : return -1;
475 0 : switch (type) {
476 0 : case AudioDeviceType::PLAYBACK:
477 0 : return audiodriver_->getIndexPlayback();
478 0 : case AudioDeviceType::RINGTONE:
479 0 : return audiodriver_->getIndexRingtone();
480 0 : case AudioDeviceType::CAPTURE:
481 0 : return audiodriver_->getIndexCapture();
482 0 : default:
483 0 : return -1;
484 : }
485 : }
486 :
487 : void
488 64 : Manager::ManagerPimpl::processRemainingParticipants(Conference& conf)
489 : {
490 64 : const std::string currentCallId(base_.getCurrentCallId());
491 64 : CallIdSet subcalls(conf.getSubCalls());
492 64 : const size_t n = subcalls.size();
493 256 : JAMI_DEBUG("[conf:{}] Processing {} remaining participant(s)", conf.getConfId(), conf.getConferenceInfos().size());
494 :
495 64 : if (n > 1) {
496 : // Reset ringbuffer's readpointers
497 30 : for (const auto& p : subcalls) {
498 20 : if (auto call = base_.getCallFromCallID(p)) {
499 20 : auto medias = call->getAudioStreams();
500 40 : for (const auto& media : medias) {
501 80 : JAMI_DEBUG("[call:{}] Remove local audio {}", p, media.first);
502 20 : base_.getRingBufferPool().flush(media.first);
503 : }
504 40 : }
505 : }
506 :
507 30 : base_.getRingBufferPool().flush(RingBufferPool::DEFAULT_ID);
508 : } else {
509 54 : if (auto acc = std::dynamic_pointer_cast<JamiAccount>(conf.getAccount())) {
510 : // Stay in a conference if 1 participants for swarm and rendezvous
511 54 : if (auto* cm = acc->convModule(true)) {
512 162 : if (acc->isRendezVous() || cm->isHosting("", conf.getConfId())) {
513 : // Check if attached
514 10 : if (conf.getState() == Conference::State::ACTIVE_ATTACHED) {
515 3 : return;
516 : }
517 : }
518 : }
519 54 : }
520 51 : if (n == 1) {
521 : // this call is the last participant (non swarm-call), hence
522 : // the conference is over
523 27 : auto p = subcalls.begin();
524 27 : if (auto call = base_.getCallFromCallID(*p)) {
525 : // if we are not listening to this conference and not a rendez-vous
526 27 : auto w = call->getAccount();
527 27 : auto account = w.lock();
528 27 : if (!account) {
529 0 : JAMI_ERROR("[conf:{}] Account no longer available", conf.getConfId());
530 0 : return;
531 : }
532 27 : if (currentCallId != conf.getConfId())
533 3 : base_.holdCall(account->getAccountID(), call->getCallId());
534 : else
535 24 : switchCall(call->getCallId());
536 54 : }
537 :
538 108 : JAMI_DEBUG("[conf:{}] Only one participant left, removing conference", conf.getConfId());
539 27 : if (auto account = conf.getAccount())
540 27 : account->removeConference(conf.getConfId());
541 : } else {
542 96 : JAMI_DEBUG("[conf:{}] No remaining participants, removing conference", conf.getConfId());
543 24 : if (auto account = conf.getAccount())
544 24 : account->removeConference(conf.getConfId());
545 24 : unsetCurrentCall();
546 : }
547 : }
548 67 : }
549 :
550 : /**
551 : * Initialization: Main Thread
552 : */
553 : std::filesystem::path
554 0 : Manager::ManagerPimpl::retrieveConfigPath() const
555 : {
556 : // TODO: Migrate config filename from dring.yml to jami.yml.
557 0 : return fileutils::get_config_dir() / "dring.yml";
558 : }
559 :
560 : void
561 104 : Manager::ManagerPimpl::unsetCurrentCall()
562 : {
563 104 : currentCall_ = "";
564 104 : }
565 :
566 : void
567 265 : Manager::ManagerPimpl::switchCall(const std::string& id)
568 : {
569 265 : std::lock_guard m(currentCallMutex_);
570 1060 : JAMI_LOG("----- Switch current call ID to '{}' -----", not id.empty() ? id.c_str() : "none");
571 265 : currentCall_ = id;
572 265 : }
573 :
574 : void
575 91 : Manager::ManagerPimpl::addWaitingCall(const std::string& id)
576 : {
577 91 : std::lock_guard m(waitingCallsMutex_);
578 : // Enable incoming call beep if needed.
579 91 : if (audiodriver_ and waitingCalls_.empty() and not currentCall_.empty())
580 44 : audiodriver_->playIncomingCallNotification(true);
581 91 : waitingCalls_.insert(id);
582 91 : }
583 :
584 : void
585 407 : Manager::ManagerPimpl::removeWaitingCall(const std::string& id)
586 : {
587 407 : std::lock_guard m(waitingCallsMutex_);
588 407 : waitingCalls_.erase(id);
589 407 : if (audiodriver_ and waitingCalls_.empty())
590 195 : audiodriver_->playIncomingCallNotification(false);
591 407 : }
592 :
593 : void
594 0 : Manager::ManagerPimpl::loadAccount(const YAML::Node& node, int& errorCount)
595 : {
596 : using yaml_utils::parseValue;
597 : using yaml_utils::parseValueOptional;
598 :
599 0 : std::string accountid;
600 0 : parseValue(node, "id", accountid);
601 :
602 0 : std::string accountType(ACCOUNT_TYPE_SIP);
603 0 : parseValueOptional(node, "type", accountType);
604 :
605 0 : if (accountid.empty())
606 0 : return;
607 :
608 0 : if (base_.preferences.isAccountPending(accountid)) {
609 0 : JAMI_LOG("[account:{}] Removing pending account from disk", accountid);
610 0 : base_.removeAccount(accountid, true);
611 0 : cleanupAccountStorage(accountid);
612 0 : return;
613 : }
614 :
615 0 : if (auto a = base_.accountFactory.createAccount(accountType, accountid)) {
616 0 : auto config = a->buildConfig();
617 0 : config->unserialize(node);
618 0 : a->setConfig(std::move(config));
619 0 : return;
620 0 : }
621 :
622 0 : JAMI_ERROR("Failed to create account of type \"{:s}\"", accountType);
623 0 : ++errorCount;
624 0 : }
625 :
626 : void
627 0 : Manager::ManagerPimpl::cleanupAccountStorage(const std::string& accountId)
628 : {
629 0 : const auto cachePath = fileutils::get_cache_dir() / accountId;
630 0 : const auto dataPath = cachePath / "values";
631 0 : const auto idPath = fileutils::get_data_dir() / accountId;
632 0 : dhtnet::fileutils::removeAll(dataPath);
633 0 : dhtnet::fileutils::removeAll(cachePath);
634 0 : dhtnet::fileutils::removeAll(idPath, true);
635 0 : }
636 :
637 : // THREAD=VoIP
638 : void
639 0 : Manager::ManagerPimpl::sendTextMessageToConference(const Conference& conf,
640 : const std::map<std::string, std::string>& messages,
641 : const std::string& from) const noexcept
642 : {
643 0 : CallIdSet subcalls(conf.getSubCalls());
644 0 : for (const auto& callId : subcalls) {
645 : try {
646 0 : auto call = base_.getCallFromCallID(callId);
647 0 : if (not call)
648 0 : throw std::runtime_error("No associated call");
649 0 : call->sendTextMessage(messages, from);
650 0 : } catch (const std::exception& e) {
651 0 : JAMI_ERROR("[conf:{}] Failed to send message to participant {}: {}", conf.getConfId(), callId, e.what());
652 0 : }
653 : }
654 0 : }
655 :
656 : void
657 0 : Manager::bindCallToConference(Call& call, Conference& conf)
658 : {
659 0 : pimpl_->bindCallToConference(call, conf);
660 0 : }
661 :
662 : void
663 58 : Manager::ManagerPimpl::bindCallToConference(Call& call, Conference& conf)
664 : {
665 58 : const auto& callId = call.getCallId();
666 58 : const auto& confId = conf.getConfId();
667 58 : const auto& state = call.getStateStr();
668 :
669 : // ensure that calls are only in one conference at a time
670 58 : if (call.isConferenceParticipant())
671 0 : base_.detachParticipant(callId);
672 :
673 232 : JAMI_DEBUG("[call:{}] Bind to conference {} (callState={})", callId, confId, state);
674 :
675 58 : auto medias = call.getAudioStreams();
676 116 : for (const auto& media : medias) {
677 232 : JAMI_DEBUG("[call:{}] Remove local audio {}", callId, media.first);
678 58 : base_.getRingBufferPool().unBindAll(media.first);
679 : }
680 :
681 58 : conf.addSubCall(callId);
682 :
683 58 : if (state == "HOLD") {
684 0 : base_.resumeCall(call.getAccountId(), callId);
685 58 : } else if (state == "INCOMING") {
686 0 : base_.acceptCall(call);
687 58 : } else if (state == "CURRENT") {
688 0 : } else if (state == "INACTIVE") {
689 0 : base_.acceptCall(call);
690 : } else
691 0 : JAMI_WARNING("[call:{}] Call state {} unrecognized for conference", callId, state);
692 58 : }
693 :
694 : //==============================================================================
695 :
696 : Manager&
697 121702 : Manager::instance()
698 : {
699 : // Meyers singleton
700 121702 : static Manager instance;
701 :
702 : // This will give a warning that can be ignored the first time instance()
703 : // is called… subsequent warnings are more serious
704 121702 : if (not Manager::initialized)
705 516 : JAMI_WARNING("Manager accessed before initialization");
706 :
707 121684 : return instance;
708 : }
709 :
710 38 : Manager::Manager()
711 38 : : rand_(dht::crypto::getSeededRandomEngine<std::mt19937_64>())
712 38 : , preferences()
713 38 : , voipPreferences()
714 38 : , audioPreference()
715 : #ifdef ENABLE_PLUGIN
716 38 : , pluginPreferences()
717 : #endif
718 : #ifdef ENABLE_VIDEO
719 38 : , videoPreferences()
720 : #endif
721 38 : , callFactory(rand_)
722 76 : , accountFactory()
723 : {
724 : #if defined _MSC_VER
725 : gnutls_global_init();
726 : #endif
727 38 : pimpl_ = std::make_unique<ManagerPimpl>(*this);
728 38 : }
729 :
730 38 : Manager::~Manager() {}
731 :
732 : void
733 307 : Manager::setAutoAnswer(bool enable)
734 : {
735 307 : pimpl_->autoAnswer_ = enable;
736 307 : }
737 :
738 : void
739 32 : Manager::init(const std::filesystem::path& config_file, libjami::InitFlag flags)
740 : {
741 : // FIXME: this is no good
742 32 : initialized = true;
743 :
744 32 : git_libgit2_init();
745 32 : auto res = git_transport_register("git", p2p_transport_cb, nullptr);
746 32 : if (res < 0) {
747 0 : const git_error* error = giterr_last();
748 0 : JAMI_ERROR("Unable to initialize git transport: {}", error ? error->message : "(unknown)");
749 : }
750 :
751 : #ifndef WIN32
752 : // Set the max number of open files.
753 : struct rlimit nofiles;
754 32 : if (getrlimit(RLIMIT_NOFILE, &nofiles) == 0) {
755 32 : if (nofiles.rlim_cur < nofiles.rlim_max && nofiles.rlim_cur <= 1024u) {
756 0 : nofiles.rlim_cur = std::min<rlim_t>(nofiles.rlim_max, 8192u);
757 0 : setrlimit(RLIMIT_NOFILE, &nofiles);
758 : }
759 : }
760 : #endif
761 :
762 : #define PJSIP_TRY(ret) \
763 : do { \
764 : if ((ret) != PJ_SUCCESS) \
765 : throw std::runtime_error(#ret " failed"); \
766 : } while (0)
767 :
768 32 : srand(time(nullptr)); // to get random number for RANDOM_PORT
769 :
770 : // Initialize PJSIP (SIP and ICE implementation)
771 32 : PJSIP_TRY(pj_init());
772 32 : setSipLogLevel();
773 32 : PJSIP_TRY(pjlib_util_init());
774 32 : PJSIP_TRY(pjnath_init());
775 : #undef PJSIP_TRY
776 :
777 32 : setGnuTlsLogLevel();
778 32 : dhtLogLevel = getDhtLogLevel();
779 32 : dhtnetLogLevel = getDhtnetLogLevel();
780 32 : pimpl_->upnpContext_->setMappingLabel("JAMI-" + fileutils::getOrCreateLocalDeviceId());
781 :
782 128 : JAMI_LOG("Using PJSIP version: {:s} for {:s}", pj_get_version(), PJ_OS_NAME);
783 128 : JAMI_LOG("Using GnuTLS version: {:s}", gnutls_check_version(nullptr));
784 128 : JAMI_LOG("Using OpenDHT version: {:s}", dht::version());
785 128 : JAMI_LOG("Using FFmpeg version: {:s}", av_version_info());
786 32 : int git2_major = 0, git2_minor = 0, git2_rev = 0;
787 32 : if (git_libgit2_version(&git2_major, &git2_minor, &git2_rev) == 0) {
788 128 : JAMI_LOG("Using libgit2 version: {:d}.{:d}.{:d}", git2_major, git2_minor, git2_rev);
789 : }
790 :
791 : // Manager can restart without being recreated (Unit tests)
792 : // So only create the SipLink once
793 32 : pimpl_->sipLink_ = std::make_unique<SIPVoIPLink>();
794 :
795 32 : check_rename(fileutils::get_cache_dir(PACKAGE_OLD), fileutils::get_cache_dir());
796 32 : check_rename(fileutils::get_data_dir(PACKAGE_OLD), fileutils::get_data_dir());
797 32 : check_rename(fileutils::get_config_dir(PACKAGE_OLD), fileutils::get_config_dir());
798 :
799 32 : pimpl_->ice_tf_ = std::make_shared<dhtnet::IceTransportFactory>(Logger::dhtLogger());
800 :
801 32 : pimpl_->path_ = config_file.empty() ? pimpl_->retrieveConfigPath() : config_file;
802 128 : JAMI_LOG("Configuration file path: {}", pimpl_->path_);
803 :
804 : #ifdef ENABLE_PLUGIN
805 32 : pimpl_->jami_plugin_manager = std::make_unique<JamiPluginManager>();
806 : #endif
807 :
808 32 : bool no_errors = true;
809 :
810 : // manager can restart without being recreated (Unit tests)
811 32 : pimpl_->finished_ = false;
812 :
813 : // Create video manager
814 32 : if (!(flags & libjami::LIBJAMI_FLAG_NO_LOCAL_VIDEO)) {
815 32 : pimpl_->videoManager_.reset(new VideoManager);
816 : }
817 :
818 32 : if (libjami::LIBJAMI_FLAG_NO_AUTOLOAD & flags) {
819 0 : autoLoad = false;
820 0 : JAMI_DEBUG("LIBJAMI_FLAG_NO_AUTOLOAD is set, accounts will neither be loaded nor backed up");
821 : } else {
822 : try {
823 32 : no_errors = pimpl_->parseConfiguration();
824 0 : } catch (const YAML::Exception& e) {
825 0 : JAMI_ERROR("[config] Failed to parse configuration: {}", e.what());
826 0 : no_errors = false;
827 0 : }
828 :
829 : // always back up last error-free configuration
830 32 : if (no_errors) {
831 29 : make_backup(pimpl_->path_);
832 : } else {
833 : // restore previous configuration
834 12 : JAMI_WARNING("Restoring last working configuration");
835 :
836 : try {
837 : // remove accounts from broken configuration
838 3 : removeAccounts();
839 3 : restore_backup(pimpl_->path_);
840 3 : pimpl_->parseConfiguration();
841 0 : } catch (const YAML::Exception& e) {
842 0 : JAMI_ERROR("{}", e.what());
843 0 : JAMI_WARNING("Restoring backup failed");
844 0 : }
845 : }
846 : }
847 :
848 : // loadAccountMap() creates the codec container when the configuration is loaded;
849 : // create a default one when it isn't (load failed or NO_AUTOLOAD set).
850 32 : if (!pimpl_->systemCodecContainer_) {
851 0 : pimpl_->systemCodecContainer_ = std::make_shared<SystemCodecContainer>();
852 0 : pimpl_->systemCodecContainer_->init(false);
853 : }
854 :
855 32 : if (!(flags & libjami::LIBJAMI_FLAG_NO_LOCAL_AUDIO)) {
856 32 : std::lock_guard lock(pimpl_->audioLayerMutex_);
857 32 : pimpl_->initAudioDriver();
858 32 : if (pimpl_->audiodriver_) {
859 32 : auto format = pimpl_->audiodriver_->getFormat();
860 32 : pimpl_->toneCtrl_.setSampleRate(format.sample_rate, format.sampleFormat);
861 96 : pimpl_->dtmfKey_.reset(new DTMF(getRingBufferPool().getInternalSamplingRate(),
862 64 : getRingBufferPool().getInternalAudioFormat().sampleFormat));
863 : }
864 32 : }
865 :
866 : // Start ASIO event loop
867 64 : pimpl_->ioContextRunner_ = std::thread([context = pimpl_->ioContext_]() {
868 : try {
869 32 : auto work = asio::make_work_guard(*context);
870 32 : context->run();
871 32 : } catch (const std::exception& ex) {
872 0 : JAMI_ERROR("[io] Unexpected io_context thread exception: {}", ex.what());
873 0 : }
874 64 : });
875 :
876 32 : if (libjami::LIBJAMI_FLAG_NO_AUTOLOAD & flags) {
877 0 : JAMI_DEBUG("LIBJAMI_FLAG_NO_AUTOLOAD is set, accounts and conversations will not be loaded");
878 0 : return;
879 : } else {
880 32 : registerAccounts();
881 : }
882 : }
883 :
884 : void
885 307 : Manager::finish() noexcept
886 : {
887 307 : bool expected = false;
888 307 : if (not pimpl_->finished_.compare_exchange_strong(expected, true))
889 269 : return;
890 :
891 : try {
892 : // Terminate UPNP context
893 38 : upnpContext()->shutdown();
894 :
895 : // Forbid call creation
896 38 : callFactory.forbid();
897 :
898 : // End all remaining active calls
899 152 : JAMI_LOG("End {} remaining call(s)", callFactory.callCount());
900 38 : for (const auto& call : callFactory.getAllCalls())
901 38 : hangupCall(call->getAccountId(), call->getCallId());
902 38 : callFactory.clear();
903 :
904 38 : for (const auto& account : getAllAccounts<JamiAccount>()) {
905 0 : if (account->getRegistrationState() == RegistrationState::INITIALIZING)
906 0 : removeAccount(account->getAccountID(), true);
907 38 : }
908 :
909 38 : saveConfig();
910 :
911 : // Disconnect accounts, close link stacks and free allocated ressources
912 38 : unregisterAccounts();
913 38 : accountFactory.clear();
914 :
915 : {
916 38 : std::lock_guard lock(pimpl_->audioLayerMutex_);
917 38 : pimpl_->audiodriver_.reset();
918 38 : }
919 :
920 152 : JAMI_DEBUG("Stopping schedulers and worker threads");
921 :
922 : // Flush remaining tasks (free lambda' with capture)
923 38 : dht::ThreadPool::io().join();
924 38 : dht::ThreadPool::computation().join();
925 :
926 : // IceTransportFactory should be stopped after the io pool
927 : // as some ICE are destroyed in a ioPool (see ConnectionManager)
928 : // Also, it must be called before pj_shutdown to avoid any problem
929 38 : pimpl_->ice_tf_.reset();
930 :
931 : // NOTE: sipLink_->shutdown() is needed because this will perform
932 : // sipTransportBroker->shutdown(); which will call Manager::instance().sipVoIPLink()
933 : // so the pointer MUST NOT be resetted at this point
934 38 : if (pimpl_->sipLink_) {
935 32 : pimpl_->sipLink_->shutdown();
936 32 : pimpl_->sipLink_.reset();
937 : }
938 :
939 38 : pj_shutdown();
940 38 : pimpl_->gitTransports_.clear();
941 38 : git_libgit2_shutdown();
942 :
943 38 : if (!pimpl_->ioContext_->stopped()) {
944 38 : pimpl_->ioContext_->stop(); // make thread stop
945 : }
946 38 : if (pimpl_->ioContextRunner_.joinable())
947 32 : pimpl_->ioContextRunner_.join();
948 :
949 : #if defined _MSC_VER
950 : gnutls_global_deinit();
951 : #endif
952 :
953 0 : } catch (const VoipLinkException& err) {
954 0 : JAMI_ERROR("[voip] {}", err.what());
955 0 : }
956 : }
957 :
958 : void
959 0 : Manager::monitor(bool continuous)
960 : {
961 0 : Logger::setMonitorLog(true);
962 0 : JAMI_DEBUG("############## START MONITORING ##############");
963 0 : JAMI_DEBUG("Using PJSIP version: {} for {}", pj_get_version(), PJ_OS_NAME);
964 0 : JAMI_DEBUG("Using GnuTLS version: {}", gnutls_check_version(nullptr));
965 0 : JAMI_DEBUG("Using OpenDHT version: {}", dht::version());
966 :
967 : #ifdef __linux__
968 : #if defined(__ANDROID__)
969 : #else
970 0 : auto opened_files = dhtnet::fileutils::readDirectory("/proc/" + std::to_string(getpid()) + "/fd").size();
971 0 : JAMI_DEBUG("Opened files: {}", opened_files);
972 : #endif
973 : #endif
974 :
975 0 : for (const auto& call : callFactory.getAllCalls())
976 0 : call->monitor();
977 0 : for (const auto& account : getAllAccounts())
978 0 : if (auto acc = std::dynamic_pointer_cast<JamiAccount>(account))
979 0 : acc->monitor();
980 0 : JAMI_DEBUG("############## END MONITORING ##############");
981 0 : Logger::setMonitorLog(continuous);
982 0 : }
983 :
984 : std::vector<std::map<std::string, std::string>>
985 0 : Manager::getConnectionList(const std::string& accountId, const std::string& conversationId)
986 : {
987 0 : std::vector<std::map<std::string, std::string>> connectionsList;
988 :
989 0 : if (accountId.empty()) {
990 0 : for (const auto& account : getAllAccounts<JamiAccount>()) {
991 0 : if (account->getRegistrationState() != RegistrationState::INITIALIZING) {
992 0 : const auto& cnl = account->getConnectionList(conversationId);
993 0 : connectionsList.insert(connectionsList.end(), cnl.begin(), cnl.end());
994 0 : }
995 0 : }
996 : } else {
997 0 : auto account = getAccount(accountId);
998 0 : if (account) {
999 0 : if (auto acc = std::dynamic_pointer_cast<JamiAccount>(account)) {
1000 0 : if (acc->getRegistrationState() != RegistrationState::INITIALIZING) {
1001 0 : const auto& cnl = acc->getConnectionList(conversationId);
1002 0 : connectionsList.insert(connectionsList.end(), cnl.begin(), cnl.end());
1003 0 : }
1004 0 : }
1005 : }
1006 0 : }
1007 :
1008 0 : return connectionsList;
1009 0 : }
1010 :
1011 : std::vector<std::map<std::string, std::string>>
1012 0 : Manager::getChannelList(const std::string& accountId, const std::string& connectionId)
1013 : {
1014 : // if account id is empty, return all channels
1015 : // else return only for specific accountid
1016 0 : std::vector<std::map<std::string, std::string>> channelsList;
1017 :
1018 0 : if (accountId.empty()) {
1019 0 : for (const auto& account : getAllAccounts<JamiAccount>()) {
1020 0 : if (account->getRegistrationState() != RegistrationState::INITIALIZING) {
1021 : // add to channelsList all channels for this account
1022 0 : const auto& cnl = account->getChannelList(connectionId);
1023 0 : channelsList.insert(channelsList.end(), cnl.begin(), cnl.end());
1024 0 : }
1025 0 : }
1026 :
1027 : }
1028 :
1029 : else {
1030 : // get the jamiaccount for this accountid and return its channels
1031 0 : auto account = getAccount(accountId);
1032 0 : if (account) {
1033 0 : if (auto acc = std::dynamic_pointer_cast<JamiAccount>(account)) {
1034 0 : if (acc->getRegistrationState() != RegistrationState::INITIALIZING) {
1035 0 : const auto& cnl = acc->getChannelList(connectionId);
1036 0 : channelsList.insert(channelsList.end(), cnl.begin(), cnl.end());
1037 0 : }
1038 0 : }
1039 : }
1040 0 : }
1041 :
1042 0 : return channelsList;
1043 0 : }
1044 :
1045 : bool
1046 334 : Manager::isCurrentCall(const Call& call) const
1047 : {
1048 334 : return pimpl_->currentCall_ == call.getCallId();
1049 : }
1050 :
1051 : bool
1052 269 : Manager::hasCurrentCall() const
1053 : {
1054 877 : for (const auto& call : callFactory.getAllCalls()) {
1055 755 : if (!call->isSubcall() && call->getStateStr() == libjami::Call::StateEvent::CURRENT)
1056 147 : return true;
1057 269 : }
1058 122 : return false;
1059 : }
1060 :
1061 : std::shared_ptr<Call>
1062 101 : Manager::getCurrentCall() const
1063 : {
1064 101 : return getCallFromCallID(pimpl_->currentCall_);
1065 : }
1066 :
1067 : const std::string&
1068 70 : Manager::getCurrentCallId() const
1069 : {
1070 70 : return pimpl_->currentCall_;
1071 : }
1072 :
1073 : void
1074 38 : Manager::unregisterAccounts()
1075 : {
1076 38 : for (const auto& account : getAllAccounts()) {
1077 0 : if (account->isEnabled()) {
1078 0 : account->doUnregister(true);
1079 : }
1080 38 : }
1081 38 : }
1082 :
1083 : ///////////////////////////////////////////////////////////////////////////////
1084 : // Management of events' IP-phone user
1085 : ///////////////////////////////////////////////////////////////////////////////
1086 : /* Main Thread */
1087 :
1088 : std::string
1089 111 : Manager::outgoingCall(const std::string& account_id,
1090 : const std::string& to,
1091 : const std::vector<libjami::MediaMap>& mediaList)
1092 : {
1093 444 : JAMI_LOG("Attempt outgoing call to '{}' with account '{}'", to, account_id);
1094 :
1095 111 : std::shared_ptr<Call> call;
1096 :
1097 : try {
1098 111 : call = newOutgoingCall(trim(to), account_id, mediaList);
1099 0 : } catch (const std::exception& e) {
1100 0 : JAMI_ERROR("{}", e.what());
1101 0 : return {};
1102 0 : }
1103 :
1104 111 : if (not call)
1105 11 : return {};
1106 :
1107 100 : stopTone();
1108 :
1109 100 : pimpl_->switchCall(call->getCallId());
1110 :
1111 100 : return call->getCallId();
1112 111 : }
1113 :
1114 : // THREAD=Main : for outgoing Call
1115 : bool
1116 77 : Manager::acceptCall(const std::string& accountId,
1117 : const std::string& callId,
1118 : const std::vector<libjami::MediaMap>& mediaList)
1119 : {
1120 77 : if (auto account = getAccount(accountId)) {
1121 77 : if (auto call = account->getCall(callId)) {
1122 77 : return acceptCall(*call, mediaList);
1123 77 : }
1124 77 : }
1125 0 : return false;
1126 : }
1127 :
1128 : bool
1129 91 : Manager::acceptCall(Call& call, const std::vector<libjami::MediaMap>& mediaList)
1130 : {
1131 364 : JAMI_LOG("Answer call {}", call.getCallId());
1132 :
1133 91 : if (call.getConnectionState() != Call::ConnectionState::RINGING) {
1134 : // The call is already answered
1135 0 : return true;
1136 : }
1137 :
1138 : // If ringing
1139 91 : stopTone();
1140 91 : pimpl_->removeWaitingCall(call.getCallId());
1141 :
1142 : try {
1143 91 : call.answer(mediaList);
1144 0 : } catch (const std::runtime_error& e) {
1145 0 : JAMI_ERROR("[call:{}] Failed to answer: {}", call.getCallId(), e.what());
1146 0 : return false;
1147 0 : }
1148 :
1149 : // if we dragged this call into a conference already
1150 91 : if (auto conf = call.getConference())
1151 0 : pimpl_->switchCall(conf->getConfId());
1152 : else
1153 91 : pimpl_->switchCall(call.getCallId());
1154 :
1155 91 : addAudio(call);
1156 :
1157 : // Start recording if set in preference
1158 91 : if (audioPreference.getIsAlwaysRecording()) {
1159 0 : auto recResult = call.toggleRecording();
1160 0 : emitSignal<libjami::CallSignal::RecordPlaybackFilepath>(call.getCallId(), call.getPath());
1161 0 : emitSignal<libjami::CallSignal::RecordingStateChanged>(call.getCallId(), recResult);
1162 : }
1163 91 : return true;
1164 : }
1165 :
1166 : // THREAD=Main
1167 : bool
1168 112 : Manager::hangupCall(const std::string& accountId, const std::string& callId)
1169 : {
1170 112 : auto account = getAccount(accountId);
1171 112 : if (not account)
1172 0 : return false;
1173 : // store the current call id
1174 112 : stopTone();
1175 112 : pimpl_->removeWaitingCall(callId);
1176 :
1177 : /* We often get here when the call was hungup before being created */
1178 112 : auto call = account->getCall(callId);
1179 112 : if (not call) {
1180 0 : JAMI_WARNING("Unable to hang up nonexistent call {}", callId);
1181 0 : return false;
1182 : }
1183 :
1184 : // Disconnect streams
1185 112 : removeAudio(*call);
1186 :
1187 112 : if (call->isConferenceParticipant()) {
1188 51 : removeParticipant(*call);
1189 : } else {
1190 : // we are not participating in a conference, current call switched to ""
1191 61 : if (isCurrentCall(*call))
1192 26 : pimpl_->unsetCurrentCall();
1193 : }
1194 :
1195 : try {
1196 112 : call->hangup(0);
1197 0 : } catch (const VoipLinkException& e) {
1198 0 : JAMI_ERROR("[call:{}] Failed to hangup: {}", call->getCallId(), e.what());
1199 0 : return false;
1200 0 : }
1201 :
1202 112 : return true;
1203 112 : }
1204 :
1205 : bool
1206 29 : Manager::hangupConference(const std::string& accountId, const std::string& confId)
1207 : {
1208 29 : if (auto account = getAccount(accountId)) {
1209 29 : if (auto conference = account->getConference(confId)) {
1210 28 : return pimpl_->hangupConference(*conference);
1211 : } else {
1212 4 : JAMI_ERROR("[conf:{}] Conference not found", confId);
1213 29 : }
1214 29 : }
1215 1 : return false;
1216 : }
1217 :
1218 : // THREAD=Main
1219 : bool
1220 6 : Manager::holdCall(const std::string&, const std::string& callId)
1221 : {
1222 6 : bool result = true;
1223 :
1224 6 : stopTone();
1225 :
1226 6 : std::string current_callId(getCurrentCallId());
1227 :
1228 6 : if (auto call = getCallFromCallID(callId)) {
1229 : try {
1230 6 : result = call->hold([=](bool ok) {
1231 6 : if (!ok) {
1232 0 : JAMI_ERROR("CallID {} holdCall failed", callId);
1233 0 : return;
1234 : }
1235 6 : removeAudio(*call); // Unbind calls in main buffer
1236 : // Remove call from the queue if it was still there
1237 6 : pimpl_->removeWaitingCall(callId);
1238 :
1239 : // Keeps current call ID if the action does not hold this call
1240 : // or a new outgoing call. This could happen in case of a conference
1241 6 : if (current_callId == callId)
1242 1 : pimpl_->unsetCurrentCall();
1243 : });
1244 0 : } catch (const VoipLinkException& e) {
1245 0 : JAMI_ERROR("[call:{}] Failed to hold: {}", callId, e.what());
1246 0 : result = false;
1247 0 : }
1248 : } else {
1249 0 : JAMI_LOG("CallID {} doesn't exist in call holdCall", callId);
1250 0 : return false;
1251 6 : }
1252 :
1253 6 : return result;
1254 6 : }
1255 :
1256 : // THREAD=Main
1257 : bool
1258 3 : Manager::resumeCall(const std::string&, const std::string& callId)
1259 : {
1260 3 : bool result = true;
1261 :
1262 3 : stopTone();
1263 :
1264 3 : std::shared_ptr<Call> call = getCallFromCallID(callId);
1265 3 : if (!call)
1266 0 : return false;
1267 :
1268 : try {
1269 3 : result = call->resume([=](bool ok) {
1270 3 : if (!ok) {
1271 0 : JAMI_ERROR("CallID {} resumeCall failed", callId);
1272 0 : return;
1273 : }
1274 :
1275 3 : if (auto conf = call->getConference())
1276 0 : pimpl_->switchCall(conf->getConfId());
1277 : else
1278 3 : pimpl_->switchCall(call->getCallId());
1279 :
1280 3 : addAudio(*call);
1281 : });
1282 0 : } catch (const VoipLinkException& e) {
1283 0 : JAMI_ERROR("[call] Failed to resume: {}", e.what());
1284 0 : return false;
1285 0 : }
1286 :
1287 3 : return result;
1288 3 : }
1289 :
1290 : // THREAD=Main
1291 : bool
1292 2 : Manager::transferCall(const std::string& accountId, const std::string& callId, const std::string& to)
1293 : {
1294 2 : auto account = getAccount(accountId);
1295 2 : if (not account)
1296 0 : return false;
1297 2 : if (auto call = account->getCall(callId)) {
1298 2 : if (call->isConferenceParticipant())
1299 0 : removeParticipant(*call);
1300 2 : call->transfer(to);
1301 : } else
1302 2 : return false;
1303 :
1304 : // remove waiting call in case we make transfer without even answer
1305 2 : pimpl_->removeWaitingCall(callId);
1306 :
1307 2 : return true;
1308 2 : }
1309 :
1310 : void
1311 0 : Manager::transferFailed()
1312 : {
1313 0 : emitSignal<libjami::CallSignal::TransferFailed>();
1314 0 : }
1315 :
1316 : void
1317 0 : Manager::transferSucceeded()
1318 : {
1319 0 : emitSignal<libjami::CallSignal::TransferSucceeded>();
1320 0 : }
1321 :
1322 : // THREAD=Main : Call:Incoming
1323 : bool
1324 2 : Manager::refuseCall(const std::string& accountId, const std::string& id)
1325 : {
1326 2 : if (auto account = getAccount(accountId)) {
1327 2 : if (auto call = account->getCall(id)) {
1328 2 : stopTone();
1329 2 : call->refuse();
1330 2 : pimpl_->removeWaitingCall(id);
1331 2 : removeAudio(*call);
1332 2 : return true;
1333 2 : }
1334 2 : }
1335 0 : return false;
1336 : }
1337 :
1338 : bool
1339 0 : Manager::holdConference(const std::string& accountId, const std::string& confId)
1340 : {
1341 0 : JAMI_LOG("[conf:{}] Hold conference", confId);
1342 :
1343 0 : if (const auto account = getAccount(accountId)) {
1344 0 : if (auto conf = account->getConference(confId)) {
1345 0 : conf->detachHost();
1346 0 : emitSignal<libjami::CallSignal::ConferenceChanged>(accountId, conf->getConfId(), conf->getStateStr());
1347 0 : return true;
1348 0 : }
1349 0 : }
1350 0 : return false;
1351 : }
1352 :
1353 : bool
1354 0 : Manager::resumeConference(const std::string& accountId, const std::string& confId)
1355 : {
1356 0 : JAMI_DEBUG("[conf:{}] Resume conference", confId);
1357 :
1358 0 : if (const auto account = getAccount(accountId)) {
1359 0 : if (auto conf = account->getConference(confId)) {
1360 : // Resume conf only if it was in hold state otherwise…
1361 : // all participants are restarted
1362 0 : if (conf->getState() == Conference::State::HOLD) {
1363 0 : for (const auto& item : conf->getSubCalls())
1364 0 : resumeCall(accountId, item);
1365 :
1366 0 : pimpl_->switchCall(confId);
1367 0 : conf->setState(Conference::State::ACTIVE_ATTACHED);
1368 0 : emitSignal<libjami::CallSignal::ConferenceChanged>(accountId, conf->getConfId(), conf->getStateStr());
1369 0 : return true;
1370 0 : } else if (conf->getState() == Conference::State::ACTIVE_DETACHED) {
1371 0 : pimpl_->addMainParticipant(*conf);
1372 : }
1373 0 : }
1374 0 : }
1375 0 : return false;
1376 : }
1377 :
1378 : bool
1379 12 : Manager::addSubCall(const std::string& accountId,
1380 : const std::string& callId,
1381 : const std::string& account2Id,
1382 : const std::string& conferenceId)
1383 : {
1384 12 : auto account = getAccount(accountId);
1385 12 : auto account2 = getAccount(account2Id);
1386 12 : if (account && account2) {
1387 12 : auto call = account->getCall(callId);
1388 12 : auto conf = account2->getConference(conferenceId);
1389 12 : if (!call or !conf)
1390 0 : return false;
1391 12 : auto callConf = call->getConference();
1392 12 : if (callConf != conf)
1393 12 : return addSubCall(*call, *conf);
1394 36 : }
1395 0 : return false;
1396 12 : }
1397 :
1398 : bool
1399 12 : Manager::addSubCall(Call& call, Conference& conference)
1400 : {
1401 48 : JAMI_DEBUG("[conf:{}] Adding participant {}", conference.getConfId(), call.getCallId());
1402 :
1403 : // Store the current call ID (it will change in resumeCall or in acceptCall)
1404 12 : pimpl_->bindCallToConference(call, conference);
1405 :
1406 : // Don't attach current user yet
1407 12 : if (conference.getState() == Conference::State::ACTIVE_DETACHED) {
1408 0 : return true;
1409 : }
1410 :
1411 : // TODO: remove this ugly hack → There should be different calls when double clicking
1412 : // a conference to add main participant to it, or (in this case) adding a participant
1413 : // to conference
1414 12 : pimpl_->unsetCurrentCall();
1415 12 : pimpl_->addMainParticipant(conference);
1416 12 : pimpl_->switchCall(conference.getConfId());
1417 12 : addAudio(call);
1418 :
1419 12 : return true;
1420 : }
1421 :
1422 : void
1423 12 : Manager::ManagerPimpl::addMainParticipant(Conference& conf)
1424 : {
1425 48 : JAMI_DEBUG("[conf:{}] Adding main participant", conf.getConfId());
1426 12 : conf.attachHost(conf.getLastMediaList());
1427 12 : emitSignal<libjami::CallSignal::ConferenceChanged>(conf.getAccountId(), conf.getConfId(), conf.getStateStr());
1428 12 : switchCall(conf.getConfId());
1429 12 : }
1430 :
1431 : bool
1432 28 : Manager::ManagerPimpl::hangupConference(Conference& conference)
1433 : {
1434 112 : JAMI_DEBUG("[conf:{}] Hanging up conference", conference.getConfId());
1435 28 : CallIdSet subcalls(conference.getSubCalls());
1436 28 : conference.detachHost();
1437 28 : if (subcalls.empty()) {
1438 5 : if (auto account = conference.getAccount())
1439 5 : account->removeConference(conference.getConfId());
1440 : }
1441 77 : for (const auto& callId : subcalls) {
1442 49 : if (auto call = base_.getCallFromCallID(callId))
1443 49 : base_.hangupCall(call->getAccountId(), callId);
1444 : }
1445 28 : unsetCurrentCall();
1446 28 : return true;
1447 28 : }
1448 :
1449 : bool
1450 0 : Manager::addMainParticipant(const std::string& accountId, const std::string& conferenceId)
1451 : {
1452 0 : JAMI_LOG("[conf:{}] Adding main participant", conferenceId);
1453 :
1454 0 : if (auto account = getAccount(accountId)) {
1455 0 : if (auto conf = account->getConference(conferenceId)) {
1456 0 : pimpl_->addMainParticipant(*conf);
1457 0 : return true;
1458 : } else
1459 0 : JAMI_WARNING("[conf:{}] Failed to add main participant (conference not found)", conferenceId);
1460 0 : }
1461 0 : return false;
1462 : }
1463 :
1464 : std::shared_ptr<Call>
1465 615 : Manager::getCallFromCallID(const std::string& callID) const
1466 : {
1467 615 : return callFactory.getCall(callID);
1468 : }
1469 :
1470 : bool
1471 23 : Manager::joinParticipant(const std::string& accountId,
1472 : const std::string& callId1,
1473 : const std::string& account2Id,
1474 : const std::string& callId2,
1475 : bool attached)
1476 : {
1477 92 : JAMI_DEBUG("Joining participants {} and {}, attached={}", callId1, callId2, attached);
1478 23 : auto account = getAccount(accountId);
1479 23 : auto account2 = getAccount(account2Id);
1480 23 : if (not account or not account2) {
1481 0 : return false;
1482 : }
1483 :
1484 92 : JAMI_LOG("Creating conference for participants {} and {}, host attached: {}", callId1, callId2, attached);
1485 :
1486 23 : if (callId1 == callId2) {
1487 0 : JAMI_ERROR("Unable to join participant {} to itself", callId1);
1488 0 : return false;
1489 : }
1490 :
1491 : // Set corresponding conference ids for call 1
1492 23 : auto call1 = account->getCall(callId1);
1493 23 : if (!call1) {
1494 0 : JAMI_ERROR("Unable to find call {}", callId1);
1495 0 : return false;
1496 : }
1497 :
1498 : // Set corresponding conference details
1499 23 : auto call2 = account2->getCall(callId2);
1500 23 : if (!call2) {
1501 0 : JAMI_ERROR("Unable to find call {}", callId2);
1502 0 : return false;
1503 : }
1504 :
1505 23 : auto mediaAttr = call1->getMediaAttributeList();
1506 23 : if (mediaAttr.empty()) {
1507 0 : JAMI_WARNING("[call:{}] No media attribute found, using media attribute from call [{}]", callId1, callId2);
1508 0 : mediaAttr = call2->getMediaAttributeList();
1509 : }
1510 :
1511 : // Filter out secondary audio streams that are muted: these are SDP
1512 : // negotiation artifacts (the host answered a participant's extra audio
1513 : // offer with a muted slot) and do not represent real host audio sources.
1514 : {
1515 23 : bool audioFound = false;
1516 23 : mediaAttr.erase(std::remove_if(mediaAttr.begin(),
1517 : mediaAttr.end(),
1518 42 : [&audioFound](const MediaAttribute& attr) {
1519 42 : if (attr.type_ == MediaType::MEDIA_AUDIO) {
1520 23 : if (audioFound && attr.muted_)
1521 0 : return true; // remove secondary audio streams
1522 23 : audioFound = true;
1523 : }
1524 42 : return false;
1525 : }),
1526 23 : mediaAttr.end());
1527 : }
1528 :
1529 92 : JAMI_DEBUG("[call:{}] Media attributes for conference:", callId1);
1530 65 : for (const auto& media : mediaAttr) {
1531 168 : JAMI_DEBUG("- {}", media.toString(true));
1532 : }
1533 :
1534 23 : auto conf = std::make_shared<Conference>(account);
1535 23 : conf->attachHost(MediaAttribute::mediaAttributesToMediaMaps(mediaAttr));
1536 23 : account->attach(conf);
1537 23 : emitSignal<libjami::CallSignal::ConferenceCreated>(account->getAccountID(), "", conf->getConfId());
1538 :
1539 : // Bind calls according to their state
1540 23 : pimpl_->bindCallToConference(*call1, *conf);
1541 23 : pimpl_->bindCallToConference(*call2, *conf);
1542 :
1543 : // Switch current call id to this conference
1544 23 : if (attached) {
1545 23 : pimpl_->switchCall(conf->getConfId());
1546 23 : conf->setState(Conference::State::ACTIVE_ATTACHED);
1547 : } else {
1548 0 : conf->detachHost();
1549 : }
1550 23 : emitSignal<libjami::CallSignal::ConferenceChanged>(account->getAccountID(), conf->getConfId(), conf->getStateStr());
1551 :
1552 23 : return true;
1553 23 : }
1554 :
1555 : void
1556 0 : Manager::createConfFromParticipantList(const std::string& accountId, const std::vector<std::string>& participantList)
1557 : {
1558 0 : auto account = getAccount(accountId);
1559 0 : if (not account) {
1560 0 : JAMI_WARNING("[account:{}] Account not found", accountId);
1561 0 : return;
1562 : }
1563 :
1564 : // we must have at least 2 participant for a conference
1565 0 : if (participantList.size() <= 1) {
1566 0 : JAMI_ERROR("[conf] Participant number must be greater than or equal to 2");
1567 0 : return;
1568 : }
1569 :
1570 0 : auto conf = std::make_shared<Conference>(account);
1571 : // attach host with empty medialist
1572 : // which will result in a default list set by initSourcesForHost
1573 0 : conf->attachHost({});
1574 :
1575 0 : unsigned successCounter = 0;
1576 0 : for (const auto& numberaccount : participantList) {
1577 0 : std::string tostr(numberaccount.substr(0, numberaccount.find(',')));
1578 0 : std::string account(numberaccount.substr(numberaccount.find(',') + 1, numberaccount.size()));
1579 :
1580 0 : pimpl_->unsetCurrentCall();
1581 :
1582 : // Create call
1583 0 : auto callId = outgoingCall(account, tostr, {});
1584 0 : if (callId.empty())
1585 0 : continue;
1586 :
1587 : // Manager methods may behave differently if the call id participates in a conference
1588 0 : conf->addSubCall(callId);
1589 0 : successCounter++;
1590 0 : }
1591 :
1592 : // Create the conference if and only if at least 2 calls have been successfully created
1593 0 : if (successCounter >= 2) {
1594 0 : account->attach(conf);
1595 0 : emitSignal<libjami::CallSignal::ConferenceCreated>(accountId, "", conf->getConfId());
1596 : }
1597 0 : }
1598 :
1599 : bool
1600 0 : Manager::detachHost(const std::shared_ptr<Conference>& conf)
1601 : {
1602 0 : if (not conf)
1603 0 : return false;
1604 :
1605 0 : JAMI_LOG("[conf:{}] Detaching host", conf->getConfId());
1606 0 : conf->detachHost();
1607 0 : emitSignal<libjami::CallSignal::ConferenceChanged>(conf->getAccountId(), conf->getConfId(), conf->getStateStr());
1608 0 : pimpl_->unsetCurrentCall();
1609 0 : return true;
1610 : }
1611 :
1612 : bool
1613 0 : Manager::detachParticipant(const std::string& callId)
1614 : {
1615 0 : JAMI_DEBUG("Detaching participant {}", callId);
1616 :
1617 0 : auto call = getCallFromCallID(callId);
1618 0 : if (!call) {
1619 0 : JAMI_ERROR("Unable to find call {}", callId);
1620 0 : return false;
1621 : }
1622 :
1623 : // Don't hold ringing calls when detaching them from conferences
1624 0 : if (call->getStateStr() != "RINGING")
1625 0 : holdCall(call->getAccountId(), callId);
1626 :
1627 0 : removeParticipant(*call);
1628 0 : return true;
1629 0 : }
1630 :
1631 : void
1632 64 : Manager::removeParticipant(Call& call)
1633 : {
1634 256 : JAMI_DEBUG("Removing participant {}", call.getCallId());
1635 :
1636 64 : auto conf = call.getConference();
1637 64 : if (not conf) {
1638 0 : JAMI_ERROR("[call:{}] No conference associated, unable to remove participant", call.getCallId());
1639 0 : return;
1640 : }
1641 :
1642 64 : conf->removeSubCall(call.getCallId());
1643 :
1644 64 : removeAudio(call);
1645 :
1646 64 : emitSignal<libjami::CallSignal::ConferenceChanged>(conf->getAccountId(), conf->getConfId(), conf->getStateStr());
1647 :
1648 64 : pimpl_->processRemainingParticipants(*conf);
1649 64 : }
1650 :
1651 : bool
1652 0 : Manager::joinConference(const std::string& accountId,
1653 : const std::string& confId1,
1654 : const std::string& account2Id,
1655 : const std::string& confId2)
1656 : {
1657 0 : auto account = getAccount(accountId);
1658 0 : auto account2 = getAccount(account2Id);
1659 0 : if (not account) {
1660 0 : JAMI_ERROR("Unable to find account: {}", accountId);
1661 0 : return false;
1662 : }
1663 0 : if (not account2) {
1664 0 : JAMI_ERROR("Unable to find account: {}", account2Id);
1665 0 : return false;
1666 : }
1667 :
1668 0 : auto conf = account->getConference(confId1);
1669 0 : if (not conf) {
1670 0 : JAMI_ERROR("[conf:{}] Invalid conference ID", confId1);
1671 0 : return false;
1672 : }
1673 :
1674 0 : auto conf2 = account2->getConference(confId2);
1675 0 : if (not conf2) {
1676 0 : JAMI_ERROR("[conf:{}] Invalid conference ID", confId2);
1677 0 : return false;
1678 : }
1679 :
1680 0 : CallIdSet subcalls(conf->getSubCalls());
1681 :
1682 0 : std::vector<std::shared_ptr<Call>> calls;
1683 0 : calls.reserve(subcalls.size());
1684 :
1685 : // Detach and remove all participant from conf1 before add
1686 : // ... to conf2
1687 0 : for (const auto& callId : subcalls) {
1688 0 : JAMI_DEBUG("Detach participant {}", callId);
1689 0 : if (auto call = account->getCall(callId)) {
1690 0 : conf->removeSubCall(callId);
1691 0 : removeAudio(*call);
1692 0 : calls.emplace_back(std::move(call));
1693 : } else {
1694 0 : JAMI_ERROR("Unable to find call {}", callId);
1695 0 : }
1696 : }
1697 : // Remove conf1
1698 0 : account->removeConference(confId1);
1699 :
1700 0 : for (const auto& c : calls)
1701 0 : addSubCall(*c, *conf2);
1702 :
1703 0 : return true;
1704 0 : }
1705 :
1706 : void
1707 203 : Manager::addAudio(Call& call)
1708 : {
1709 203 : if (call.isConferenceParticipant())
1710 12 : return;
1711 191 : const auto& callId = call.getCallId();
1712 764 : JAMI_LOG("Add audio to call {}", callId);
1713 :
1714 : // bind to main
1715 191 : auto medias = call.getAudioStreams();
1716 385 : for (const auto& media : medias) {
1717 776 : JAMI_DEBUG("[call:{}] Attach audio stream {}", callId, media.first);
1718 582 : getRingBufferPool().bindRingBuffers(media.first, RingBufferPool::DEFAULT_ID);
1719 : }
1720 191 : auto oldGuard = std::move(call.audioGuard);
1721 191 : call.audioGuard = startAudioStream(AudioDeviceType::PLAYBACK);
1722 :
1723 191 : std::lock_guard lock(pimpl_->audioLayerMutex_);
1724 191 : if (!pimpl_->audiodriver_) {
1725 0 : JAMI_ERROR("Uninitialized audio driver");
1726 0 : return;
1727 : }
1728 191 : pimpl_->audiodriver_->flushUrgent();
1729 191 : getRingBufferPool().flushAllBuffers();
1730 191 : }
1731 :
1732 : void
1733 378 : Manager::removeAudio(Call& call)
1734 : {
1735 378 : const auto& callId = call.getCallId();
1736 378 : auto medias = call.getAudioStreams();
1737 759 : for (const auto& media : medias) {
1738 1524 : JAMI_DEBUG("[call:{}] Remove local audio {}", callId, media.first);
1739 381 : getRingBufferPool().unBindAll(media.first);
1740 : }
1741 378 : }
1742 :
1743 : std::shared_ptr<asio::io_context>
1744 27301 : Manager::ioContext() const
1745 : {
1746 27301 : return pimpl_->ioContext_;
1747 : }
1748 :
1749 : std::shared_ptr<dhtnet::upnp::UPnPContext>
1750 831 : Manager::upnpContext() const
1751 : {
1752 831 : return pimpl_->upnpContext_;
1753 : }
1754 :
1755 : void
1756 1009 : Manager::saveConfig(const std::shared_ptr<Account>& acc)
1757 : {
1758 1009 : if (auto account = std::dynamic_pointer_cast<JamiAccount>(acc))
1759 985 : account->saveConfig();
1760 : else
1761 1009 : saveConfig();
1762 1009 : }
1763 :
1764 : void
1765 2455 : Manager::saveConfig()
1766 : {
1767 9820 : JAMI_LOG("Saving configuration to '{}'", pimpl_->path_);
1768 :
1769 2455 : if (pimpl_->audiodriver_) {
1770 2449 : audioPreference.setVolumemic(pimpl_->audiodriver_->getCaptureGain());
1771 2449 : audioPreference.setVolumespkr(pimpl_->audiodriver_->getPlaybackGain());
1772 2449 : audioPreference.setCaptureMuted(pimpl_->audiodriver_->isCaptureMuted());
1773 2449 : audioPreference.setPlaybackMuted(pimpl_->audiodriver_->isPlaybackMuted());
1774 : }
1775 :
1776 : try {
1777 2455 : YAML::Emitter out;
1778 :
1779 : // FIXME maybe move this into accountFactory?
1780 2455 : out << YAML::BeginMap << YAML::Key << "accounts";
1781 2455 : out << YAML::Value << YAML::BeginSeq;
1782 :
1783 8504 : for (const auto& account : accountFactory.getAllAccounts()) {
1784 6049 : if (auto jamiAccount = std::dynamic_pointer_cast<JamiAccount>(account)) {
1785 5868 : auto accountConfig = jamiAccount->getPath() / "config.yml";
1786 5868 : if (not std::filesystem::is_regular_file(accountConfig)) {
1787 0 : saveConfig(jamiAccount);
1788 : }
1789 5868 : } else {
1790 181 : account->config().serialize(out);
1791 6049 : }
1792 2455 : }
1793 2455 : out << YAML::EndSeq;
1794 :
1795 : // FIXME: this is a hack until we get rid of accountOrder
1796 2455 : preferences.verifyAccountOrder(getAccountList());
1797 2455 : preferences.serialize(out);
1798 2455 : voipPreferences.serialize(out);
1799 2455 : audioPreference.serialize(out);
1800 : #ifdef ENABLE_VIDEO
1801 2455 : videoPreferences.serialize(out);
1802 : #endif
1803 : #ifdef ENABLE_PLUGIN
1804 2455 : pluginPreferences.serialize(out);
1805 : #endif
1806 :
1807 2455 : std::lock_guard lock(dhtnet::fileutils::getFileLock(pimpl_->path_));
1808 2455 : std::ofstream fout(pimpl_->path_);
1809 2455 : fout.write(out.c_str(), static_cast<long>(out.size()));
1810 2455 : } catch (const YAML::Exception& e) {
1811 0 : JAMI_ERROR("[config] YAML error: {}", e.what());
1812 0 : } catch (const std::runtime_error& e) {
1813 0 : JAMI_ERROR("[config] {}", e.what());
1814 0 : }
1815 2455 : }
1816 :
1817 : // THREAD=Main | VoIPLink
1818 : void
1819 0 : Manager::playDtmf(char code)
1820 : {
1821 0 : stopTone();
1822 :
1823 0 : if (not voipPreferences.getPlayDtmf()) {
1824 0 : return;
1825 : }
1826 :
1827 : // length in milliseconds
1828 0 : int pulselen = voipPreferences.getPulseLength();
1829 :
1830 0 : if (pulselen == 0) {
1831 0 : return;
1832 : }
1833 :
1834 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
1835 :
1836 : // fast return, no sound, so no dtmf
1837 0 : if (not pimpl_->audiodriver_ or not pimpl_->dtmfKey_) {
1838 0 : return;
1839 : }
1840 :
1841 0 : std::shared_ptr<AudioDeviceGuard> audioGuard = startAudioStream(AudioDeviceType::PLAYBACK);
1842 0 : if (not pimpl_->audiodriver_->waitForStart(std::chrono::seconds(1))) {
1843 0 : JAMI_ERROR("[audio] Failed to start audio layer for DTMF");
1844 0 : return;
1845 : }
1846 :
1847 : // number of data sampling in one pulselen depends on samplerate
1848 : // size (n sampling) = time_ms * sampling/s
1849 : // ---------------------
1850 : // ms/s
1851 0 : unsigned size = (unsigned) ((pulselen * (long) pimpl_->audiodriver_->getSampleRate()) / 1000ul);
1852 0 : if (!pimpl_->dtmfBuf_ or pimpl_->dtmfBuf_->getFrameSize() != size)
1853 0 : pimpl_->dtmfBuf_ = std::make_shared<AudioFrame>(pimpl_->audiodriver_->getFormat(), size);
1854 :
1855 : // Handle dtmf
1856 0 : pimpl_->dtmfKey_->startTone(code);
1857 :
1858 : // copy the sound
1859 0 : if (pimpl_->dtmfKey_->generateDTMF(pimpl_->dtmfBuf_->pointer())) {
1860 : // Put buffer to urgentRingBuffer
1861 : // put the size in bytes…
1862 : // so size * 1 channel (mono) * sizeof (bytes for the data)
1863 : // audiolayer->flushUrgent();
1864 :
1865 0 : pimpl_->audiodriver_->putUrgent(pimpl_->dtmfBuf_);
1866 : }
1867 :
1868 0 : auto dtmfTimer = std::make_unique<asio::steady_timer>(*pimpl_->ioContext_, std::chrono::milliseconds(pulselen));
1869 0 : dtmfTimer->async_wait([this, audioGuard, t = dtmfTimer.get()](const asio::error_code& ec) {
1870 0 : if (ec)
1871 0 : return;
1872 0 : JAMI_LOG("End of dtmf");
1873 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
1874 0 : if (pimpl_->dtmfTimer_.get() == t)
1875 0 : pimpl_->dtmfTimer_.reset();
1876 0 : });
1877 0 : if (pimpl_->dtmfTimer_)
1878 0 : pimpl_->dtmfTimer_->cancel();
1879 0 : pimpl_->dtmfTimer_ = std::move(dtmfTimer);
1880 0 : }
1881 :
1882 : // Multi-thread
1883 : bool
1884 115 : Manager::incomingCallsWaiting()
1885 : {
1886 115 : std::lock_guard m(pimpl_->waitingCallsMutex_);
1887 230 : return not pimpl_->waitingCalls_.empty();
1888 115 : }
1889 :
1890 : void
1891 101 : Manager::incomingCall(const std::string& accountId, Call& call)
1892 : {
1893 101 : if (not accountId.empty()) {
1894 101 : pimpl_->stripSipPrefix(call);
1895 : }
1896 :
1897 101 : auto const& account = getAccount(accountId);
1898 101 : if (not account) {
1899 0 : JAMI_ERROR("Incoming call {} on unknown account {}", call.getCallId(), accountId);
1900 0 : return;
1901 : }
1902 :
1903 : // Process the call.
1904 101 : pimpl_->processIncomingCall(accountId, call);
1905 101 : }
1906 :
1907 : void
1908 0 : Manager::incomingMessage(const std::string& accountId,
1909 : const std::string& callId,
1910 : const std::string& from,
1911 : const std::map<std::string, std::string>& messages)
1912 : {
1913 0 : auto account = getAccount(accountId);
1914 0 : if (not account) {
1915 0 : return;
1916 : }
1917 0 : if (auto call = account->getCall(callId)) {
1918 0 : if (call->isConferenceParticipant()) {
1919 0 : if (auto conf = call->getConference()) {
1920 : // filter out vcards messages as they could be resent by master as its own vcard
1921 : // TODO. Implement a protocol to handle vcard messages
1922 0 : bool sendToOtherParicipants = true;
1923 0 : for (auto& message : messages) {
1924 0 : if (message.first.find("x-ring/ring.profile.vcard") != std::string::npos) {
1925 0 : sendToOtherParicipants = false;
1926 : }
1927 : }
1928 0 : if (sendToOtherParicipants) {
1929 0 : pimpl_->sendTextMessageToConference(*conf, messages, from);
1930 : }
1931 :
1932 : // in case of a conference we must notify client using conference id
1933 0 : emitSignal<libjami::CallSignal::IncomingMessage>(accountId, conf->getConfId(), from, messages);
1934 : } else {
1935 0 : JAMI_ERROR("[call:{}] No conference associated to call", callId);
1936 0 : }
1937 : } else {
1938 0 : emitSignal<libjami::CallSignal::IncomingMessage>(accountId, callId, from, messages);
1939 : }
1940 0 : }
1941 0 : }
1942 :
1943 : void
1944 0 : Manager::sendCallTextMessage(const std::string& accountId,
1945 : const std::string& callID,
1946 : const std::map<std::string, std::string>& messages,
1947 : const std::string& from,
1948 : bool /*isMixed TODO: use it */)
1949 : {
1950 0 : auto account = getAccount(accountId);
1951 0 : if (not account) {
1952 0 : return;
1953 : }
1954 :
1955 0 : if (auto conf = account->getConference(callID)) {
1956 0 : pimpl_->sendTextMessageToConference(*conf, messages, from);
1957 0 : } else if (auto call = account->getCall(callID)) {
1958 0 : if (call->isConferenceParticipant()) {
1959 0 : if (auto conf = call->getConference()) {
1960 0 : pimpl_->sendTextMessageToConference(*conf, messages, from);
1961 : } else {
1962 0 : JAMI_ERROR("[call:{}] No conference associated to call", callID);
1963 0 : }
1964 : } else {
1965 : try {
1966 0 : call->sendTextMessage(messages, from);
1967 0 : } catch (const im::InstantMessageException& e) {
1968 0 : JAMI_ERROR("Failed to send message to call {}: {}", call->getCallId(), e.what());
1969 0 : }
1970 : }
1971 : } else {
1972 0 : JAMI_ERROR("Failed to send message to {}: nonexistent call ID", callID);
1973 0 : }
1974 0 : }
1975 :
1976 : // THREAD=VoIP CALL=Outgoing
1977 : void
1978 90 : Manager::peerAnsweredCall(Call& call)
1979 : {
1980 90 : const auto& callId = call.getCallId();
1981 360 : JAMI_LOG("[call:{}] Peer answered", callId);
1982 :
1983 : // The if statement is useful only if we sent two calls at the same time.
1984 90 : if (isCurrentCall(call))
1985 0 : stopTone();
1986 :
1987 90 : addAudio(call);
1988 :
1989 90 : if (pimpl_->audiodriver_) {
1990 90 : std::lock_guard lock(pimpl_->audioLayerMutex_);
1991 90 : getRingBufferPool().flushAllBuffers();
1992 90 : pimpl_->audiodriver_->flushUrgent();
1993 90 : }
1994 :
1995 90 : if (audioPreference.getIsAlwaysRecording()) {
1996 0 : auto result = call.toggleRecording();
1997 0 : emitSignal<libjami::CallSignal::RecordPlaybackFilepath>(callId, call.getPath());
1998 0 : emitSignal<libjami::CallSignal::RecordingStateChanged>(callId, result);
1999 : }
2000 90 : }
2001 :
2002 : // THREAD=VoIP Call=Outgoing
2003 : void
2004 178 : Manager::peerRingingCall(Call& call)
2005 : {
2006 712 : JAMI_LOG("[call:{}] Peer ringing", call.getCallId());
2007 178 : if (!hasCurrentCall())
2008 67 : ringback();
2009 178 : }
2010 :
2011 : // THREAD=VoIP Call=Outgoing/Ingoing
2012 : void
2013 99 : Manager::peerHungupCall(Call& call)
2014 : {
2015 99 : const auto& callId = call.getCallId();
2016 396 : JAMI_LOG("[call:{}] Peer hung up", callId);
2017 :
2018 99 : if (call.isConferenceParticipant()) {
2019 11 : removeParticipant(call);
2020 88 : } else if (isCurrentCall(call)) {
2021 9 : stopTone();
2022 9 : pimpl_->unsetCurrentCall();
2023 : }
2024 :
2025 99 : call.peerHungup();
2026 :
2027 99 : pimpl_->removeWaitingCall(callId);
2028 99 : if (not incomingCallsWaiting())
2029 68 : stopTone();
2030 :
2031 99 : removeAudio(call);
2032 99 : }
2033 :
2034 : // THREAD=VoIP
2035 : void
2036 0 : Manager::callBusy(Call& call)
2037 : {
2038 0 : JAMI_LOG("[call:{}] Busy", call.getCallId());
2039 :
2040 0 : if (isCurrentCall(call)) {
2041 0 : pimpl_->unsetCurrentCall();
2042 : }
2043 :
2044 0 : pimpl_->removeWaitingCall(call.getCallId());
2045 0 : if (not incomingCallsWaiting())
2046 0 : stopTone();
2047 0 : }
2048 :
2049 : // THREAD=VoIP
2050 : void
2051 95 : Manager::callFailure(Call& call)
2052 : {
2053 380 : JAMI_LOG("[call:{}] {} failed", call.getCallId(), call.isSubcall() ? "Sub-call" : "Parent call");
2054 :
2055 95 : if (isCurrentCall(call)) {
2056 4 : pimpl_->unsetCurrentCall();
2057 : }
2058 :
2059 95 : if (call.isConferenceParticipant()) {
2060 8 : JAMI_LOG("[call:{}] Participating in conference, removing participant", call.getCallId());
2061 : // remove this participant
2062 2 : removeParticipant(call);
2063 : }
2064 :
2065 95 : pimpl_->removeWaitingCall(call.getCallId());
2066 95 : if (not call.isSubcall() && not incomingCallsWaiting())
2067 4 : stopTone();
2068 95 : removeAudio(call);
2069 95 : }
2070 :
2071 : /**
2072 : * Multi Thread
2073 : */
2074 : void
2075 496 : Manager::stopTone()
2076 : {
2077 496 : if (not voipPreferences.getPlayTones())
2078 0 : return;
2079 :
2080 496 : pimpl_->toneCtrl_.stop();
2081 496 : pimpl_->toneDeviceGuard_.reset();
2082 : }
2083 :
2084 : /**
2085 : * Multi Thread
2086 : */
2087 : void
2088 0 : Manager::playTone()
2089 : {
2090 0 : pimpl_->playATone(Tone::ToneId::DIALTONE);
2091 0 : }
2092 :
2093 : /**
2094 : * Multi Thread
2095 : */
2096 : void
2097 0 : Manager::playToneWithMessage()
2098 : {
2099 0 : pimpl_->playATone(Tone::ToneId::CONGESTION);
2100 0 : }
2101 :
2102 : /**
2103 : * Multi Thread
2104 : */
2105 : void
2106 0 : Manager::congestion()
2107 : {
2108 0 : pimpl_->playATone(Tone::ToneId::CONGESTION);
2109 0 : }
2110 :
2111 : /**
2112 : * Multi Thread
2113 : */
2114 : void
2115 122 : Manager::ringback()
2116 : {
2117 122 : pimpl_->playATone(Tone::ToneId::RINGTONE);
2118 122 : }
2119 :
2120 : /**
2121 : * Multi Thread
2122 : */
2123 : void
2124 55 : Manager::playRingtone(const std::string& accountID)
2125 : {
2126 55 : const auto account = getAccount(accountID);
2127 55 : if (!account) {
2128 0 : JAMI_WARNING("[account:{}] Invalid account for ringtone", accountID);
2129 0 : return;
2130 : }
2131 :
2132 55 : if (!account->getRingtoneEnabled()) {
2133 0 : ringback();
2134 0 : return;
2135 : }
2136 :
2137 : {
2138 55 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2139 :
2140 55 : if (not pimpl_->audiodriver_) {
2141 0 : JAMI_ERROR("[audio] No audio layer for ringtone");
2142 0 : return;
2143 : }
2144 : // start audio if not started AND flush all buffers (main and urgent)
2145 55 : auto oldGuard = std::move(pimpl_->toneDeviceGuard_);
2146 55 : pimpl_->toneDeviceGuard_ = startAudioStream(AudioDeviceType::RINGTONE);
2147 55 : auto format = pimpl_->audiodriver_->getFormat();
2148 55 : pimpl_->toneCtrl_.setSampleRate(format.sample_rate, format.sampleFormat);
2149 55 : }
2150 :
2151 55 : if (not pimpl_->toneCtrl_.setAudioFile(account->getRingtonePath().string()))
2152 55 : ringback();
2153 55 : }
2154 :
2155 : std::shared_ptr<AudioLoop>
2156 0 : Manager::getTelephoneTone()
2157 : {
2158 0 : return pimpl_->toneCtrl_.getTelephoneTone();
2159 : }
2160 :
2161 : std::shared_ptr<AudioLoop>
2162 0 : Manager::getTelephoneFile()
2163 : {
2164 0 : return pimpl_->toneCtrl_.getTelephoneFile();
2165 : }
2166 :
2167 : /**
2168 : * Set input audio plugin
2169 : */
2170 : void
2171 0 : Manager::setAudioPlugin(const std::string& audioPlugin)
2172 : {
2173 : {
2174 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2175 0 : audioPreference.setAlsaPlugin(audioPlugin);
2176 0 : pimpl_->audiodriver_.reset();
2177 0 : pimpl_->initAudioDriver();
2178 0 : }
2179 : // Recreate audio driver with new settings
2180 0 : saveConfig();
2181 0 : }
2182 :
2183 : /**
2184 : * Set audio output device
2185 : */
2186 : void
2187 0 : Manager::setAudioDevice(int index, AudioDeviceType type)
2188 : {
2189 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2190 :
2191 0 : if (not pimpl_->audiodriver_) {
2192 0 : JAMI_ERROR("[audio] Uninitialized audio driver");
2193 0 : return;
2194 : }
2195 0 : if (pimpl_->getCurrentDeviceIndex(type) == index) {
2196 0 : JAMI_DEBUG("[audio] Audio device already selected, doing nothing");
2197 0 : return;
2198 : }
2199 :
2200 0 : pimpl_->audiodriver_->updatePreference(audioPreference, index, type);
2201 :
2202 : // Recreate audio driver with new settings
2203 0 : pimpl_->audiodriver_.reset();
2204 0 : pimpl_->initAudioDriver();
2205 0 : saveConfig();
2206 0 : }
2207 :
2208 : /**
2209 : * Get list of supported audio output device
2210 : */
2211 : std::vector<std::string>
2212 0 : Manager::getAudioOutputDeviceList()
2213 : {
2214 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2215 :
2216 0 : if (not pimpl_->audiodriver_) {
2217 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
2218 0 : return {};
2219 : }
2220 :
2221 0 : return pimpl_->audiodriver_->getPlaybackDeviceList();
2222 0 : }
2223 :
2224 : /**
2225 : * Get list of supported audio input device
2226 : */
2227 : std::vector<std::string>
2228 0 : Manager::getAudioInputDeviceList()
2229 : {
2230 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2231 :
2232 0 : if (not pimpl_->audiodriver_) {
2233 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
2234 0 : return {};
2235 : }
2236 :
2237 0 : return pimpl_->audiodriver_->getCaptureDeviceList();
2238 0 : }
2239 :
2240 : /**
2241 : * Get string array representing integer indexes of output and input device
2242 : */
2243 : std::vector<std::string>
2244 0 : Manager::getCurrentAudioDevicesIndex()
2245 : {
2246 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2247 0 : if (not pimpl_->audiodriver_) {
2248 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
2249 0 : return {};
2250 : }
2251 :
2252 0 : return {std::to_string(pimpl_->audiodriver_->getIndexPlayback()),
2253 0 : std::to_string(pimpl_->audiodriver_->getIndexCapture()),
2254 0 : std::to_string(pimpl_->audiodriver_->getIndexRingtone())};
2255 0 : }
2256 :
2257 616 : AudioDeviceGuard::AudioDeviceGuard(Manager& manager, AudioDeviceType type)
2258 616 : : manager_(manager)
2259 616 : , type_(type)
2260 : {
2261 616 : auto streamId = (unsigned) type;
2262 1232 : if (streamId >= manager_.pimpl_->audioStreamUsers_.size())
2263 0 : throw std::invalid_argument("Invalid audio device type");
2264 616 : if (manager_.pimpl_->audioStreamUsers_[streamId]++ == 0) {
2265 223 : if (auto layer = manager_.getAudioDriver())
2266 223 : layer->startStream(type);
2267 : }
2268 616 : }
2269 :
2270 0 : AudioDeviceGuard::AudioDeviceGuard(Manager& manager, const std::string& captureDevice)
2271 0 : : manager_(manager)
2272 0 : , type_(AudioDeviceType::CAPTURE)
2273 0 : , captureDevice_(captureDevice)
2274 : {
2275 0 : std::lock_guard lock(manager_.pimpl_->audioDeviceUsersMutex_);
2276 0 : auto& users = manager_.pimpl_->audioDeviceUsers_[captureDevice];
2277 0 : if (users++ == 0) {
2278 0 : if (auto layer = manager_.getAudioDriver()) {
2279 0 : layer->startCaptureStream(captureDevice);
2280 0 : }
2281 : }
2282 0 : }
2283 :
2284 1232 : AudioDeviceGuard::~AudioDeviceGuard()
2285 : {
2286 616 : if (captureDevice_.empty()) {
2287 616 : auto streamId = (unsigned) type_;
2288 616 : if (--manager_.pimpl_->audioStreamUsers_[streamId] == 0) {
2289 223 : if (auto layer = manager_.getAudioDriver())
2290 223 : layer->stopStream(type_);
2291 : }
2292 : } else {
2293 0 : std::lock_guard lock(manager_.pimpl_->audioDeviceUsersMutex_);
2294 0 : auto it = manager_.pimpl_->audioDeviceUsers_.find(captureDevice_);
2295 0 : if (it != manager_.pimpl_->audioDeviceUsers_.end()) {
2296 0 : if (--it->second == 0) {
2297 0 : if (auto layer = manager_.getAudioDriver())
2298 0 : layer->stopCaptureStream(captureDevice_);
2299 0 : manager_.pimpl_->audioDeviceUsers_.erase(it);
2300 : }
2301 : }
2302 0 : }
2303 616 : }
2304 :
2305 : bool
2306 0 : Manager::getIsAlwaysRecording() const
2307 : {
2308 0 : return audioPreference.getIsAlwaysRecording();
2309 : }
2310 :
2311 : void
2312 0 : Manager::setIsAlwaysRecording(bool isAlwaysRec)
2313 : {
2314 0 : audioPreference.setIsAlwaysRecording(isAlwaysRec);
2315 0 : saveConfig();
2316 0 : }
2317 :
2318 : bool
2319 4 : Manager::toggleRecordingCall(const std::string& accountId, const std::string& id)
2320 : {
2321 4 : bool result = false;
2322 4 : if (auto account = getAccount(accountId)) {
2323 4 : std::shared_ptr<Recordable> rec;
2324 4 : if (auto conf = account->getConference(id)) {
2325 8 : JAMI_DEBUG("[conf:{}] Toggling recording", id);
2326 2 : rec = conf;
2327 2 : } else if (auto call = account->getCall(id)) {
2328 8 : JAMI_DEBUG("[call:{}] Toggling recording", id);
2329 2 : rec = call;
2330 : } else {
2331 0 : JAMI_ERROR("Unable to find recordable instance {}", id);
2332 0 : return false;
2333 6 : }
2334 4 : result = rec->toggleRecording();
2335 4 : emitSignal<libjami::CallSignal::RecordPlaybackFilepath>(id, rec->getPath());
2336 4 : emitSignal<libjami::CallSignal::RecordingStateChanged>(id, result);
2337 8 : }
2338 4 : return result;
2339 : }
2340 :
2341 : bool
2342 0 : Manager::startRecordedFilePlayback(const std::string& filepath)
2343 : {
2344 0 : JAMI_DEBUG("[audio] Start recorded file playback: {}", filepath);
2345 :
2346 : {
2347 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2348 :
2349 0 : if (not pimpl_->audiodriver_) {
2350 0 : JAMI_ERROR("[audio] No audio layer for recorded file playback");
2351 0 : return false;
2352 : }
2353 :
2354 0 : auto oldGuard = std::move(pimpl_->toneDeviceGuard_);
2355 0 : pimpl_->toneDeviceGuard_ = startAudioStream(AudioDeviceType::RINGTONE);
2356 0 : auto format = pimpl_->audiodriver_->getFormat();
2357 0 : pimpl_->toneCtrl_.setSampleRate(format.sample_rate, format.sampleFormat);
2358 0 : }
2359 :
2360 0 : return pimpl_->toneCtrl_.setAudioFile(filepath);
2361 : }
2362 :
2363 : void
2364 0 : Manager::recordingPlaybackSeek(const double value)
2365 : {
2366 0 : pimpl_->toneCtrl_.seek(value);
2367 0 : }
2368 :
2369 : void
2370 0 : Manager::stopRecordedFilePlayback()
2371 : {
2372 0 : JAMI_DEBUG("[audio] Stop recorded file playback");
2373 :
2374 0 : pimpl_->toneCtrl_.stopAudioFile();
2375 0 : pimpl_->toneDeviceGuard_.reset();
2376 0 : }
2377 :
2378 : void
2379 0 : Manager::setHistoryLimit(int days)
2380 : {
2381 0 : JAMI_DEBUG("[config] Set history limit to {} days", days);
2382 0 : preferences.setHistoryLimit(days);
2383 0 : saveConfig();
2384 0 : }
2385 :
2386 : int
2387 0 : Manager::getHistoryLimit() const
2388 : {
2389 0 : return preferences.getHistoryLimit();
2390 : }
2391 :
2392 : void
2393 0 : Manager::setRingingTimeout(std::chrono::seconds timeout)
2394 : {
2395 0 : JAMI_DEBUG("[config] Set ringing timeout to {} seconds", timeout);
2396 0 : preferences.setRingingTimeout(timeout);
2397 0 : saveConfig();
2398 0 : }
2399 :
2400 : std::chrono::seconds
2401 101 : Manager::getRingingTimeout() const
2402 : {
2403 101 : return preferences.getRingingTimeout();
2404 : }
2405 :
2406 : bool
2407 0 : Manager::setAudioManager(const std::string& api)
2408 : {
2409 : {
2410 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2411 :
2412 0 : if (not pimpl_->audiodriver_)
2413 0 : return false;
2414 :
2415 0 : if (api == audioPreference.getAudioApi()) {
2416 0 : JAMI_DEBUG("[audio] Audio manager '{}' already in use", api);
2417 0 : return true;
2418 : }
2419 0 : }
2420 :
2421 : {
2422 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2423 0 : audioPreference.setAudioApi(api);
2424 0 : pimpl_->audiodriver_.reset();
2425 0 : pimpl_->initAudioDriver();
2426 0 : }
2427 :
2428 0 : saveConfig();
2429 :
2430 : // ensure that we completed the transition (i.e. no fallback was used)
2431 0 : return api == audioPreference.getAudioApi();
2432 : }
2433 :
2434 : std::string
2435 0 : Manager::getAudioManager() const
2436 : {
2437 0 : return audioPreference.getAudioApi();
2438 : }
2439 :
2440 : int
2441 0 : Manager::getAudioInputDeviceIndex(const std::string& name)
2442 : {
2443 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2444 :
2445 0 : if (not pimpl_->audiodriver_) {
2446 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
2447 0 : return 0;
2448 : }
2449 :
2450 0 : return pimpl_->audiodriver_->getAudioDeviceIndex(name, AudioDeviceType::CAPTURE);
2451 0 : }
2452 :
2453 : int
2454 0 : Manager::getAudioOutputDeviceIndex(const std::string& name)
2455 : {
2456 0 : std::lock_guard lock(pimpl_->audioLayerMutex_);
2457 :
2458 0 : if (not pimpl_->audiodriver_) {
2459 0 : JAMI_ERROR("[audio] Uninitialized audio layer");
2460 0 : return 0;
2461 : }
2462 :
2463 0 : return pimpl_->audiodriver_->getAudioDeviceIndex(name, AudioDeviceType::PLAYBACK);
2464 0 : }
2465 :
2466 : std::string
2467 0 : Manager::getCurrentAudioOutputPlugin() const
2468 : {
2469 0 : return audioPreference.getAlsaPlugin();
2470 : }
2471 :
2472 : std::string
2473 0 : Manager::getNoiseSuppressState() const
2474 : {
2475 0 : return audioPreference.getNoiseReduce();
2476 : }
2477 :
2478 : void
2479 0 : Manager::setNoiseSuppressState(const std::string& state)
2480 : {
2481 0 : audioPreference.setNoiseReduce(state);
2482 0 : saveConfig();
2483 0 : }
2484 :
2485 : std::string
2486 0 : Manager::getEchoCancellationState() const
2487 : {
2488 0 : return audioPreference.getEchoCanceller();
2489 : }
2490 :
2491 : void
2492 0 : Manager::setEchoCancellationState(const std::string& state)
2493 : {
2494 0 : audioPreference.setEchoCancel(state);
2495 0 : saveConfig();
2496 0 : }
2497 :
2498 : bool
2499 0 : Manager::getVoiceActivityDetectionState() const
2500 : {
2501 0 : return audioPreference.getVadEnabled();
2502 : }
2503 :
2504 : void
2505 0 : Manager::setVoiceActivityDetectionState(bool state)
2506 : {
2507 0 : audioPreference.setVad(state);
2508 0 : saveConfig();
2509 0 : }
2510 :
2511 : bool
2512 0 : Manager::isAGCEnabled() const
2513 : {
2514 0 : return audioPreference.isAGCEnabled();
2515 : }
2516 :
2517 : void
2518 0 : Manager::setAGCState(bool state)
2519 : {
2520 0 : audioPreference.setAGCState(state);
2521 0 : saveConfig();
2522 0 : }
2523 :
2524 : /**
2525 : * Initialization: Main Thread
2526 : */
2527 : void
2528 32 : Manager::ManagerPimpl::initAudioDriver()
2529 : {
2530 32 : audiodriver_.reset(base_.audioPreference.createAudioLayer());
2531 32 : constexpr std::array<AudioDeviceType, 3> TYPES {AudioDeviceType::CAPTURE,
2532 : AudioDeviceType::PLAYBACK,
2533 : AudioDeviceType::RINGTONE};
2534 128 : for (const auto& type : TYPES)
2535 96 : if (audioStreamUsers_[(unsigned) type])
2536 0 : audiodriver_->startStream(type);
2537 32 : }
2538 :
2539 : // Internal helper method
2540 : void
2541 101 : Manager::ManagerPimpl::stripSipPrefix(Call& incomCall)
2542 : {
2543 : // strip sip: which is not required and causes confusion with IP-to-IP calls
2544 : // when placing new call from history.
2545 101 : std::string peerNumber(incomCall.getPeerNumber());
2546 :
2547 101 : const char SIP_PREFIX[] = "sip:";
2548 101 : size_t startIndex = peerNumber.find(SIP_PREFIX);
2549 :
2550 101 : if (startIndex != std::string::npos)
2551 0 : incomCall.setPeerNumber(peerNumber.substr(startIndex + sizeof(SIP_PREFIX) - 1));
2552 101 : }
2553 :
2554 : // Internal helper method
2555 : void
2556 101 : Manager::ManagerPimpl::processIncomingCall(const std::string& accountId, Call& incomCall)
2557 : {
2558 101 : base_.stopTone();
2559 :
2560 101 : auto incomCallId = incomCall.getCallId();
2561 101 : auto currentCall = base_.getCurrentCall();
2562 :
2563 101 : auto account = incomCall.getAccount().lock();
2564 101 : if (!account) {
2565 0 : JAMI_ERROR("[call:{}] No account detected", incomCallId);
2566 0 : return;
2567 : }
2568 :
2569 101 : auto username = incomCall.toUsername();
2570 101 : if (account->getAccountType() == ACCOUNT_TYPE_JAMI && username.find('/') != std::string::npos) {
2571 : // Avoid to do heavy stuff in SIPVoIPLink's transaction_request_cb
2572 10 : dht::ThreadPool::io().run([account, incomCallId, username]() {
2573 10 : if (auto jamiAccount = std::dynamic_pointer_cast<JamiAccount>(account))
2574 10 : jamiAccount->handleIncomingConversationCall(incomCallId, username);
2575 10 : });
2576 10 : return;
2577 : }
2578 :
2579 91 : auto const& mediaList = MediaAttribute::mediaAttributesToMediaMaps(incomCall.getMediaAttributeList());
2580 :
2581 91 : if (mediaList.empty())
2582 0 : JAMI_WARNING("Incoming call {} has an empty media list", incomCallId);
2583 :
2584 364 : JAMI_DEBUG("Incoming call {} on account {} with {} media", incomCallId, accountId, mediaList.size());
2585 :
2586 91 : emitSignal<libjami::CallSignal::IncomingCall>(accountId, incomCallId, incomCall.getPeerNumber(), mediaList);
2587 :
2588 91 : if (not base_.hasCurrentCall()) {
2589 55 : incomCall.setState(Call::ConnectionState::RINGING);
2590 : #if !(defined(TARGET_OS_IOS) && TARGET_OS_IOS)
2591 55 : if (not account->isRendezVous())
2592 55 : base_.playRingtone(accountId);
2593 : #endif
2594 : } else {
2595 36 : if (account->isDenySecondCallEnabled()) {
2596 0 : base_.refuseCall(account->getAccountID(), incomCallId);
2597 0 : return;
2598 : }
2599 : }
2600 :
2601 91 : addWaitingCall(incomCallId);
2602 :
2603 91 : if (account->isRendezVous()) {
2604 0 : dht::ThreadPool::io().run([this, account, incomCall = incomCall.shared_from_this()] {
2605 0 : base_.acceptCall(*incomCall);
2606 :
2607 0 : for (const auto& callId : account->getCallList()) {
2608 0 : if (auto call = account->getCall(callId)) {
2609 0 : if (call->getState() != Call::CallState::ACTIVE)
2610 0 : continue;
2611 0 : if (call != incomCall) {
2612 0 : if (auto conf = call->getConference()) {
2613 0 : base_.addSubCall(*incomCall, *conf);
2614 : } else {
2615 0 : base_.joinParticipant(account->getAccountID(),
2616 0 : incomCall->getCallId(),
2617 0 : account->getAccountID(),
2618 : call->getCallId(),
2619 : false);
2620 0 : }
2621 0 : return;
2622 : }
2623 0 : }
2624 0 : }
2625 :
2626 : // First call
2627 0 : auto conf = std::make_shared<Conference>(account);
2628 0 : account->attach(conf);
2629 0 : emitSignal<libjami::CallSignal::ConferenceCreated>(account->getAccountID(), "", conf->getConfId());
2630 :
2631 : // Bind calls according to their state
2632 0 : bindCallToConference(*incomCall, *conf);
2633 0 : conf->detachHost();
2634 0 : emitSignal<libjami::CallSignal::ConferenceChanged>(account->getAccountID(),
2635 0 : conf->getConfId(),
2636 : conf->getStateStr());
2637 0 : });
2638 91 : } else if (autoAnswer_ || account->isAutoAnswerEnabled()) {
2639 8 : dht::ThreadPool::io().run([this, incomCall = incomCall.shared_from_this()] { base_.acceptCall(*incomCall); });
2640 87 : } else if (currentCall && currentCall->getCallId() != incomCallId) {
2641 : // Test if already calling this person
2642 83 : auto peerNumber = incomCall.getPeerNumber();
2643 83 : auto currentPeerNumber = currentCall->getPeerNumber();
2644 332 : string_replace(peerNumber, "@ring.dht", "");
2645 249 : string_replace(currentPeerNumber, "@ring.dht", "");
2646 83 : if (currentCall->getAccountId() == account->getAccountID() && currentPeerNumber == peerNumber) {
2647 0 : auto answerToCall = false;
2648 0 : auto downgradeToAudioOnly = currentCall->isAudioOnly() != incomCall.isAudioOnly();
2649 0 : if (downgradeToAudioOnly)
2650 : // Accept the incoming audio only
2651 0 : answerToCall = incomCall.isAudioOnly();
2652 : else
2653 : // Accept the incoming call from the higher id number
2654 0 : answerToCall = (account->getUsername().compare(peerNumber) < 0);
2655 :
2656 0 : if (answerToCall) {
2657 0 : runOnMainThread([accountId = currentCall->getAccountId(),
2658 0 : currentCallID = currentCall->getCallId(),
2659 : incomCall = incomCall.shared_from_this()] {
2660 0 : auto& mgr = Manager::instance();
2661 0 : mgr.acceptCall(*incomCall);
2662 0 : mgr.hangupCall(accountId, currentCallID);
2663 0 : });
2664 : }
2665 : }
2666 83 : }
2667 131 : }
2668 :
2669 : AudioFormat
2670 101 : Manager::hardwareAudioFormatChanged(AudioFormat format)
2671 : {
2672 101 : return audioFormatUsed(format);
2673 : }
2674 :
2675 : AudioFormat
2676 101 : Manager::audioFormatUsed(AudioFormat format)
2677 : {
2678 101 : AudioFormat currentFormat = pimpl_->ringbufferpool_->getInternalAudioFormat();
2679 101 : if (currentFormat == format)
2680 101 : return format;
2681 :
2682 0 : JAMI_DEBUG("Audio format changed: {} → {}", currentFormat.toString(), format.toString());
2683 :
2684 0 : pimpl_->ringbufferpool_->setInternalAudioFormat(format);
2685 0 : pimpl_->toneCtrl_.setSampleRate(format.sample_rate, format.sampleFormat);
2686 0 : pimpl_->dtmfKey_.reset(new DTMF(format.sample_rate, format.sampleFormat));
2687 :
2688 0 : return format;
2689 : }
2690 :
2691 : void
2692 0 : Manager::setAccountsOrder(const std::string& order)
2693 : {
2694 0 : JAMI_LOG("Set accounts order: {}", order);
2695 0 : preferences.setAccountOrder(order);
2696 0 : saveConfig();
2697 0 : emitSignal<libjami::ConfigurationSignal::AccountsChanged>();
2698 0 : }
2699 :
2700 : std::vector<std::string>
2701 3531 : Manager::getAccountList() const
2702 : {
2703 : // Concatenate all account pointers in a single map
2704 3531 : std::vector<std::string> v;
2705 3531 : v.reserve(accountCount());
2706 11521 : for (const auto& account : getAllAccounts()) {
2707 7990 : v.emplace_back(account->getAccountID());
2708 3531 : }
2709 :
2710 3531 : return v;
2711 0 : }
2712 :
2713 : std::map<std::string, std::string>
2714 1 : Manager::getAccountDetails(const std::string& accountID) const
2715 : {
2716 1 : const auto account = getAccount(accountID);
2717 :
2718 1 : if (account) {
2719 1 : return account->getAccountDetails();
2720 : } else {
2721 0 : JAMI_ERROR("[account:{}] Unable to get account details on nonexistent account", accountID);
2722 : // return an empty map since unable to throw an exception to D-Bus
2723 0 : return {};
2724 : }
2725 1 : }
2726 :
2727 : std::map<std::string, std::string>
2728 2 : Manager::getVolatileAccountDetails(const std::string& accountID) const
2729 : {
2730 2 : const auto account = getAccount(accountID);
2731 :
2732 2 : if (account) {
2733 2 : return account->getVolatileAccountDetails();
2734 : } else {
2735 0 : JAMI_ERROR("[account:{}] Unable to get volatile account details on nonexistent account", accountID);
2736 0 : return {};
2737 : }
2738 2 : }
2739 :
2740 : void
2741 16 : Manager::setAccountDetails(const std::string& accountID, const std::map<std::string, std::string>& details)
2742 : {
2743 64 : JAMI_DEBUG("[account:{}] Set account details", accountID);
2744 :
2745 16 : auto account = getAccount(accountID);
2746 16 : if (not account) {
2747 0 : JAMI_ERROR("[account:{}] Unable to find account", accountID);
2748 0 : return;
2749 : }
2750 :
2751 : // Ignore if nothing has changed
2752 16 : if (details == account->getAccountDetails())
2753 0 : return;
2754 :
2755 : // Unregister before modifying any account information
2756 16 : account->doUnregister();
2757 :
2758 16 : account->setAccountDetails(details);
2759 :
2760 16 : if (account->isUsable())
2761 16 : account->doRegister();
2762 : else
2763 0 : account->doUnregister();
2764 :
2765 : // Update account details to the client side
2766 16 : emitSignal<libjami::ConfigurationSignal::AccountDetailsChanged>(accountID, details);
2767 16 : }
2768 :
2769 : std::mt19937_64
2770 1996 : Manager::getSeededRandomEngine()
2771 : {
2772 1996 : std::lock_guard l(randMutex_);
2773 3992 : return dht::crypto::getDerivedRandomEngine(rand_);
2774 1996 : }
2775 :
2776 : std::string
2777 795 : Manager::getNewAccountId()
2778 : {
2779 795 : std::string random_id;
2780 : do {
2781 795 : random_id = to_hex_string(std::uniform_int_distribution<uint64_t>()(rand_));
2782 795 : } while (getAccount(random_id));
2783 795 : return random_id;
2784 0 : }
2785 :
2786 : std::string
2787 797 : Manager::addAccount(const std::map<std::string, std::string>& details, const std::string& accountId)
2788 : {
2789 : /** @todo Deal with both the accountMap_ and the Configuration */
2790 797 : auto newAccountID = accountId.empty() ? getNewAccountId() : accountId;
2791 :
2792 : // Get the type
2793 797 : std::string_view accountType;
2794 797 : auto typeIt = details.find(Conf::CONFIG_ACCOUNT_TYPE);
2795 797 : if (typeIt != details.end())
2796 797 : accountType = typeIt->second;
2797 : else
2798 0 : accountType = AccountFactory::DEFAULT_ACCOUNT_TYPE;
2799 :
2800 3188 : JAMI_DEBUG("Adding account {:s} with type {}", newAccountID, accountType);
2801 :
2802 797 : auto newAccount = accountFactory.createAccount(accountType, newAccountID);
2803 797 : if (!newAccount) {
2804 0 : JAMI_ERROR("Unknown {:s} param when calling addAccount(): {:s}", Conf::CONFIG_ACCOUNT_TYPE, accountType);
2805 0 : return "";
2806 : }
2807 :
2808 797 : newAccount->setAccountDetails(details);
2809 797 : saveConfig(newAccount);
2810 797 : newAccount->doRegister();
2811 :
2812 797 : preferences.addAccount(newAccountID);
2813 797 : if (accountType != ACCOUNT_TYPE_SIP)
2814 773 : markAccountPending(newAccountID);
2815 :
2816 797 : emitSignal<libjami::ConfigurationSignal::AccountsChanged>();
2817 :
2818 797 : return newAccountID;
2819 797 : }
2820 :
2821 : void
2822 773 : Manager::markAccountPending(const std::string& accountId)
2823 : {
2824 773 : if (preferences.addPendingAccountId(accountId))
2825 773 : saveConfig();
2826 773 : }
2827 :
2828 : void
2829 791 : Manager::markAccountReady(const std::string& accountId)
2830 : {
2831 791 : if (preferences.removePendingAccountId(accountId))
2832 771 : saveConfig();
2833 791 : }
2834 :
2835 : void
2836 797 : Manager::removeAccount(const std::string& accountID, bool flush)
2837 : {
2838 : // Get it down and dying
2839 797 : if (const auto& remAccount = getAccount(accountID)) {
2840 797 : if (auto acc = std::dynamic_pointer_cast<JamiAccount>(remAccount)) {
2841 773 : acc->hangupCalls();
2842 797 : }
2843 797 : remAccount->doUnregister(true);
2844 797 : if (flush)
2845 797 : remAccount->flush();
2846 797 : accountFactory.removeAccount(*remAccount);
2847 797 : }
2848 :
2849 797 : preferences.removeAccount(accountID);
2850 797 : preferences.removePendingAccountId(accountID);
2851 :
2852 797 : saveConfig();
2853 :
2854 797 : emitSignal<libjami::ConfigurationSignal::AccountsChanged>();
2855 797 : }
2856 :
2857 : void
2858 3 : Manager::removeAccounts()
2859 : {
2860 3 : for (const auto& acc : getAccountList())
2861 3 : removeAccount(acc);
2862 3 : }
2863 :
2864 : std::vector<std::string_view>
2865 3684 : Manager::loadAccountOrder() const
2866 : {
2867 3684 : return split_string(preferences.getAccountOrder(), '/');
2868 : }
2869 :
2870 : int
2871 35 : Manager::loadAccountMap(const YAML::Node& node)
2872 : {
2873 35 : int errorCount = 0;
2874 : try {
2875 : // build preferences
2876 35 : preferences.unserialize(node);
2877 29 : voipPreferences.unserialize(node);
2878 29 : audioPreference.unserialize(node);
2879 : #ifdef ENABLE_VIDEO
2880 29 : videoPreferences.unserialize(node);
2881 : #endif
2882 : #ifdef ENABLE_PLUGIN
2883 29 : pluginPreferences.unserialize(node);
2884 : #endif
2885 6 : } catch (const YAML::Exception& e) {
2886 24 : JAMI_ERROR("[config] Preferences unserialize YAML exception: {}", e.what());
2887 6 : ++errorCount;
2888 6 : } catch (const std::exception& e) {
2889 0 : JAMI_ERROR("[config] Preferences unserialize exception: {}", e.what());
2890 0 : ++errorCount;
2891 0 : } catch (...) {
2892 0 : JAMI_ERROR("[config] Preferences unserialize unknown exception");
2893 0 : ++errorCount;
2894 0 : }
2895 :
2896 35 : pimpl_->systemCodecContainer_ = std::make_shared<SystemCodecContainer>();
2897 : #ifdef ENABLE_VIDEO
2898 35 : pimpl_->systemCodecContainer_->init(videoPreferences.getEncodingAccelerated());
2899 : #else
2900 : pimpl_->systemCodecContainer_->init(false);
2901 : #endif
2902 :
2903 : // load saved preferences for IP2IP account from configuration file
2904 35 : const auto& accountList = node["accounts"];
2905 :
2906 35 : for (auto& a : accountList) {
2907 0 : pimpl_->loadAccount(a, errorCount);
2908 0 : }
2909 :
2910 35 : const auto& accountBaseDir = fileutils::get_data_dir();
2911 35 : auto dirs = dhtnet::fileutils::readDirectory(accountBaseDir);
2912 :
2913 35 : std::condition_variable cv;
2914 35 : std::mutex lock;
2915 35 : size_t remaining {0};
2916 35 : std::unique_lock l(lock);
2917 70 : for (const auto& dir : dirs) {
2918 35 : if (accountFactory.hasAccount<JamiAccount>(dir)) {
2919 0 : continue;
2920 : }
2921 :
2922 35 : if (preferences.isAccountPending(dir)) {
2923 0 : JAMI_LOG("[account:{}] Removing pending account from disk", dir);
2924 0 : removeAccount(dir, true);
2925 0 : pimpl_->cleanupAccountStorage(dir);
2926 0 : continue;
2927 0 : }
2928 :
2929 35 : remaining++;
2930 70 : dht::ThreadPool::computation().run(
2931 70 : [this, dir, &cv, &remaining, &lock, configFile = accountBaseDir / dir / "config.yml"] {
2932 35 : if (std::filesystem::is_regular_file(configFile)) {
2933 : try {
2934 0 : auto configNode = YAML::LoadFile(configFile.string());
2935 0 : if (auto a = accountFactory.createAccount(JamiAccount::ACCOUNT_TYPE, dir)) {
2936 0 : auto config = a->buildConfig();
2937 0 : config->unserialize(configNode);
2938 0 : a->setConfig(std::move(config));
2939 0 : }
2940 0 : } catch (const std::exception& e) {
2941 0 : JAMI_ERROR("[account:{}] Unable to import account: {}", dir, e.what());
2942 0 : }
2943 : }
2944 35 : std::lock_guard l(lock);
2945 35 : remaining--;
2946 35 : cv.notify_one();
2947 35 : });
2948 : }
2949 105 : cv.wait(l, [&remaining] { return remaining == 0; });
2950 :
2951 : #ifdef ENABLE_PLUGIN
2952 35 : if (pluginPreferences.getPluginsEnabled()) {
2953 8 : jami::Manager::instance().getJamiPluginManager().loadPlugins();
2954 : }
2955 : #endif
2956 :
2957 35 : return errorCount;
2958 35 : }
2959 :
2960 : std::vector<std::string>
2961 0 : Manager::getCallList() const
2962 : {
2963 0 : std::vector<std::string> results;
2964 0 : for (const auto& call : callFactory.getAllCalls()) {
2965 0 : if (!call->isSubcall())
2966 0 : results.push_back(call->getCallId());
2967 0 : }
2968 0 : return results;
2969 0 : }
2970 :
2971 : void
2972 32 : Manager::registerAccounts()
2973 : {
2974 32 : for (auto& a : getAllAccounts()) {
2975 0 : if (a->isUsable())
2976 0 : a->doRegister();
2977 32 : }
2978 32 : }
2979 :
2980 : void
2981 212 : Manager::sendRegister(const std::string& accountID, bool enable)
2982 : {
2983 212 : const auto acc = getAccount(accountID);
2984 212 : if (!acc)
2985 0 : return;
2986 :
2987 212 : acc->setEnabled(enable);
2988 212 : saveConfig(acc);
2989 :
2990 212 : if (acc->isEnabled()) {
2991 43 : acc->doRegister();
2992 : } else
2993 169 : acc->doUnregister();
2994 212 : }
2995 :
2996 : uint64_t
2997 0 : Manager::sendTextMessage(const std::string& accountID,
2998 : const std::string& to,
2999 : const std::map<std::string, std::string>& payloads,
3000 : bool fromPlugin,
3001 : bool onlyConnected)
3002 : {
3003 0 : if (const auto acc = getAccount(accountID)) {
3004 : try {
3005 : #ifdef ENABLE_PLUGIN // modifies send message
3006 0 : auto& pluginChatManager = getJamiPluginManager().getChatServicesManager();
3007 0 : if (pluginChatManager.hasHandlers()) {
3008 0 : auto cm = std::make_shared<JamiMessage>(accountID, to, false, payloads, fromPlugin);
3009 0 : pluginChatManager.publishMessage(cm);
3010 0 : return acc->sendTextMessage(cm->peerId, "", cm->data, 0, onlyConnected);
3011 0 : } else
3012 : #endif // ENABLE_PLUGIN
3013 0 : return acc->sendTextMessage(to, "", payloads, 0, onlyConnected);
3014 0 : } catch (const std::exception& e) {
3015 0 : JAMI_ERROR("[account:{}] Exception during text message sending: {}", accountID, e.what());
3016 0 : }
3017 0 : }
3018 0 : return 0;
3019 : }
3020 :
3021 : int
3022 0 : Manager::getMessageStatus(uint64_t) const
3023 : {
3024 0 : JAMI_ERROR("Deprecated method. Please use status from message");
3025 0 : return 0;
3026 : }
3027 :
3028 : int
3029 0 : Manager::getMessageStatus(const std::string&, uint64_t) const
3030 : {
3031 0 : JAMI_ERROR("Deprecated method. Please use status from message");
3032 0 : return 0;
3033 : }
3034 :
3035 : void
3036 0 : Manager::setAccountActive(const std::string& accountID, bool active, bool shutdownConnections)
3037 : {
3038 0 : const auto acc = getAccount(accountID);
3039 0 : if (!acc || acc->isActive() == active)
3040 0 : return;
3041 0 : acc->setActive(active);
3042 0 : if (acc->isEnabled()) {
3043 0 : if (active) {
3044 0 : acc->doRegister();
3045 : } else {
3046 0 : acc->doUnregister(shutdownConnections);
3047 : }
3048 : }
3049 0 : emitSignal<libjami::ConfigurationSignal::VolatileDetailsChanged>(accountID, acc->getVolatileAccountDetails());
3050 0 : }
3051 :
3052 : void
3053 0 : Manager::loadAccountAndConversation(const std::string& accountId, bool loadAll, const std::string& convId)
3054 : {
3055 0 : auto account = getAccount(accountId);
3056 0 : if (!account && !autoLoad) {
3057 : /*
3058 : With the LIBJAMI_FLAG_NO_AUTOLOAD flag active, accounts are not
3059 : automatically created during manager initialization, nor are
3060 : their configurations set or backed up. This is because account
3061 : creation triggers the initialization of the certStore. There why
3062 : account creation now occurs here in response to a received notification.
3063 : */
3064 0 : const auto& accountBaseDir = fileutils::get_data_dir();
3065 0 : auto configFile = accountBaseDir / accountId / "config.yml";
3066 : try {
3067 0 : if ((account = accountFactory.createAccount(JamiAccount::ACCOUNT_TYPE, accountId))) {
3068 0 : account->enableAutoLoadConversations(false);
3069 0 : auto configNode = YAML::LoadFile(configFile.string());
3070 0 : auto config = account->buildConfig();
3071 0 : config->unserialize(configNode);
3072 0 : account->setConfig(std::move(config));
3073 0 : }
3074 0 : } catch (const std::runtime_error& e) {
3075 0 : JAMI_WARNING("[account:{}] Failed to load account: {}", accountId, e.what());
3076 0 : return;
3077 0 : }
3078 0 : }
3079 :
3080 0 : if (!account) {
3081 0 : JAMI_WARNING("[account:{}] Unable to load account", accountId);
3082 0 : return;
3083 : }
3084 :
3085 0 : if (auto jamiAcc = std::dynamic_pointer_cast<JamiAccount>(account)) {
3086 0 : jamiAcc->setActive(true);
3087 0 : jamiAcc->reloadContacts();
3088 0 : if (jamiAcc->isUsable())
3089 0 : jamiAcc->doRegister();
3090 0 : if (auto* convModule = jamiAcc->convModule()) {
3091 0 : convModule->reloadRequests();
3092 0 : if (loadAll) {
3093 0 : convModule->loadConversations();
3094 0 : } else if (!convId.empty()) {
3095 0 : jamiAcc->loadConversation(convId);
3096 : }
3097 : }
3098 0 : }
3099 0 : }
3100 :
3101 : std::shared_ptr<SystemCodecContainer>
3102 806 : Manager::getSystemCodecContainer() const
3103 : {
3104 806 : return pimpl_->systemCodecContainer_;
3105 : }
3106 :
3107 : std::shared_ptr<AudioLayer>
3108 446 : Manager::getAudioDriver()
3109 : {
3110 446 : return pimpl_->audiodriver_;
3111 : }
3112 :
3113 : std::shared_ptr<Call>
3114 113 : Manager::newOutgoingCall(std::string_view toUrl,
3115 : const std::string& accountId,
3116 : const std::vector<libjami::MediaMap>& mediaList)
3117 : {
3118 113 : auto account = getAccount(accountId);
3119 113 : if (not account) {
3120 0 : JAMI_WARNING("[account:{}] No account matches ID", accountId);
3121 0 : return {};
3122 : }
3123 :
3124 113 : if (not account->isUsable()) {
3125 0 : JAMI_WARNING("[account:{}] Account is unusable", accountId);
3126 0 : return {};
3127 : }
3128 :
3129 113 : return account->newOutgoingCall(toUrl, mediaList);
3130 113 : }
3131 :
3132 : #ifdef ENABLE_VIDEO
3133 : std::shared_ptr<video::SinkClient>
3134 238 : Manager::createSinkClient(const std::string& id, bool mixer)
3135 : {
3136 238 : std::lock_guard lk(pimpl_->sinksMutex_);
3137 238 : auto& sinkRef = pimpl_->sinkMap_[id];
3138 238 : if (auto sink = sinkRef.lock())
3139 238 : return sink;
3140 219 : auto sink = std::make_shared<video::SinkClient>(id, mixer);
3141 219 : sinkRef = sink;
3142 219 : return sink;
3143 238 : }
3144 :
3145 : void
3146 366 : Manager::createSinkClients(const std::string& callId,
3147 : const ConfInfo& infos,
3148 : const std::vector<std::shared_ptr<video::VideoFrameActiveWriter>>& videoStreams,
3149 : std::map<std::string, std::shared_ptr<video::SinkClient>>& sinksMap,
3150 : const std::string& accountId)
3151 : {
3152 366 : auto account = accountId.empty() ? nullptr : getAccount<JamiAccount>(accountId);
3153 :
3154 366 : std::set<std::string> sinkIdsList {};
3155 366 : std::vector<std::pair<std::shared_ptr<video::SinkClient>, std::pair<int, int>>> newSinks;
3156 :
3157 : // create video sinks
3158 366 : std::unique_lock lk(pimpl_->sinksMutex_);
3159 1282 : for (const auto& participant : infos) {
3160 916 : std::string sinkId = participant.sinkId;
3161 916 : if (sinkId.empty()) {
3162 170 : sinkId = callId;
3163 170 : sinkId += string_remove_suffix(participant.uri, '@') + participant.device;
3164 : }
3165 916 : if (participant.w && participant.h && !participant.videoMuted) {
3166 13 : auto& currentSinkW = pimpl_->sinkMap_[sinkId];
3167 26 : if (account && string_remove_suffix(participant.uri, '@') == account->getUsername()
3168 26 : && participant.device == account->currentDeviceId()) {
3169 : // This is a local sink that must already exist
3170 13 : continue;
3171 : }
3172 0 : if (auto currentSink = currentSinkW.lock()) {
3173 : // If sink exists, update it
3174 0 : currentSink->setCrop(participant.x, participant.y, participant.w, participant.h);
3175 0 : sinkIdsList.emplace(sinkId);
3176 0 : continue;
3177 0 : }
3178 0 : auto newSink = std::make_shared<video::SinkClient>(sinkId, false);
3179 0 : currentSinkW = newSink;
3180 0 : newSink->setCrop(participant.x, participant.y, participant.w, participant.h);
3181 0 : newSinks.emplace_back(newSink, std::make_pair(participant.w, participant.h));
3182 0 : sinksMap.emplace(sinkId, std::move(newSink));
3183 0 : sinkIdsList.emplace(sinkId);
3184 0 : } else {
3185 903 : sinkIdsList.erase(sinkId);
3186 : }
3187 916 : }
3188 366 : lk.unlock();
3189 :
3190 : // remove unused video sinks
3191 366 : for (auto it = sinksMap.begin(); it != sinksMap.end();) {
3192 0 : if (sinkIdsList.find(it->first) == sinkIdsList.end()) {
3193 0 : for (auto& videoStream : videoStreams)
3194 0 : videoStream->detach(it->second.get());
3195 0 : it->second->stop();
3196 0 : it = sinksMap.erase(it);
3197 : } else {
3198 0 : it++;
3199 : }
3200 : }
3201 :
3202 : // create new video sinks
3203 366 : for (const auto& [sink, size] : newSinks) {
3204 0 : sink->start();
3205 0 : sink->setFrameSize(size.first, size.second);
3206 0 : for (auto& videoStream : videoStreams)
3207 0 : videoStream->attach(sink.get());
3208 : }
3209 366 : }
3210 :
3211 : std::shared_ptr<video::SinkClient>
3212 2 : Manager::getSinkClient(const std::string& id)
3213 : {
3214 2 : std::lock_guard lk(pimpl_->sinksMutex_);
3215 2 : const auto& iter = pimpl_->sinkMap_.find(id);
3216 4 : if (iter != std::end(pimpl_->sinkMap_))
3217 0 : if (auto sink = iter->second.lock())
3218 0 : return sink;
3219 2 : return nullptr;
3220 2 : }
3221 : #endif // ENABLE_VIDEO
3222 :
3223 : RingBufferPool&
3224 45211 : Manager::getRingBufferPool()
3225 : {
3226 45211 : return *pimpl_->ringbufferpool_;
3227 : }
3228 :
3229 : bool
3230 0 : Manager::hasAccount(const std::string& accountID)
3231 : {
3232 0 : return accountFactory.hasAccount(accountID);
3233 : }
3234 :
3235 : const std::shared_ptr<dhtnet::IceTransportFactory>&
3236 922 : Manager::getIceTransportFactory()
3237 : {
3238 922 : return pimpl_->ice_tf_;
3239 : }
3240 :
3241 : VideoManager*
3242 4118 : Manager::getVideoManager() const
3243 : {
3244 4118 : return pimpl_->videoManager_.get();
3245 : }
3246 :
3247 : std::vector<libjami::Message>
3248 0 : Manager::getLastMessages(const std::string& accountID, const uint64_t& base_timestamp)
3249 : {
3250 0 : if (const auto acc = getAccount(accountID))
3251 0 : return acc->getLastMessages(base_timestamp);
3252 0 : return {};
3253 : }
3254 :
3255 : SIPVoIPLink&
3256 3440 : Manager::sipVoIPLink() const
3257 : {
3258 3440 : return *pimpl_->sipLink_;
3259 : }
3260 :
3261 : #ifdef ENABLE_PLUGIN
3262 : JamiPluginManager&
3263 3677 : Manager::getJamiPluginManager() const
3264 : {
3265 3677 : return *pimpl_->jami_plugin_manager;
3266 : }
3267 : #endif
3268 :
3269 : std::shared_ptr<dhtnet::ChannelSocket>
3270 2183 : Manager::gitSocket(std::string_view accountId, std::string_view deviceId, std::string_view conversationId)
3271 : {
3272 2183 : if (const auto acc = getAccount<JamiAccount>(accountId))
3273 2183 : if (auto* convModule = acc->convModule(true))
3274 2183 : return convModule->gitSocket(deviceId, conversationId);
3275 0 : return nullptr;
3276 : }
3277 :
3278 : std::map<std::string, std::string>
3279 0 : Manager::getNearbyPeers(const std::string& accountID)
3280 : {
3281 0 : if (const auto acc = getAccount<JamiAccount>(accountID))
3282 0 : return acc->getNearbyPeers();
3283 0 : return {};
3284 : }
3285 :
3286 : void
3287 0 : Manager::setDefaultModerator(const std::string& accountID, const std::string& peerURI, bool state)
3288 : {
3289 0 : auto acc = getAccount(accountID);
3290 0 : if (!acc) {
3291 0 : JAMI_ERROR("[account:{}] Failed to change default moderator: account not found", accountID);
3292 0 : return;
3293 : }
3294 :
3295 0 : if (state)
3296 0 : acc->addDefaultModerator(peerURI);
3297 : else
3298 0 : acc->removeDefaultModerator(peerURI);
3299 0 : saveConfig(acc);
3300 0 : }
3301 :
3302 : std::vector<std::string>
3303 0 : Manager::getDefaultModerators(const std::string& accountID)
3304 : {
3305 0 : auto acc = getAccount(accountID);
3306 0 : if (!acc) {
3307 0 : JAMI_ERROR("[account:{}] Failed to get default moderators: account not found", accountID);
3308 0 : return {};
3309 : }
3310 :
3311 0 : auto set = acc->getDefaultModerators();
3312 0 : return std::vector<std::string>(set.begin(), set.end());
3313 0 : }
3314 :
3315 : void
3316 0 : Manager::enableLocalModerators(const std::string& accountID, bool isModEnabled)
3317 : {
3318 0 : if (auto acc = getAccount(accountID))
3319 0 : acc->editConfig([&](AccountConfig& config) { config.localModeratorsEnabled = isModEnabled; });
3320 0 : }
3321 :
3322 : bool
3323 0 : Manager::isLocalModeratorsEnabled(const std::string& accountID)
3324 : {
3325 0 : auto acc = getAccount(accountID);
3326 0 : if (!acc) {
3327 0 : JAMI_ERROR("[account:{}] Failed to get local moderators: account not found", accountID);
3328 0 : return true; // Default value
3329 : }
3330 0 : return acc->isLocalModeratorsEnabled();
3331 0 : }
3332 :
3333 : void
3334 0 : Manager::setAllModerators(const std::string& accountID, bool allModerators)
3335 : {
3336 0 : if (auto acc = getAccount(accountID))
3337 0 : acc->editConfig([&](AccountConfig& config) { config.allModeratorsEnabled = allModerators; });
3338 0 : }
3339 :
3340 : bool
3341 0 : Manager::isAllModerators(const std::string& accountID)
3342 : {
3343 0 : auto acc = getAccount(accountID);
3344 0 : if (!acc) {
3345 0 : JAMI_ERROR("[account:{}] Failed to get all moderators: account not found", accountID);
3346 0 : return true; // Default value
3347 : }
3348 0 : return acc->isAllModerators();
3349 0 : }
3350 :
3351 : void
3352 2182 : Manager::insertGitTransport(git_smart_subtransport* tr, std::unique_ptr<P2PSubTransport>&& sub)
3353 : {
3354 2182 : std::lock_guard lk(pimpl_->gitTransportsMtx_);
3355 2183 : pimpl_->gitTransports_[tr] = std::move(sub);
3356 2183 : }
3357 :
3358 : void
3359 2183 : Manager::eraseGitTransport(git_smart_subtransport* tr)
3360 : {
3361 2183 : std::lock_guard lk(pimpl_->gitTransportsMtx_);
3362 2183 : pimpl_->gitTransports_.erase(tr);
3363 2183 : }
3364 :
3365 : dhtnet::tls::CertificateStore&
3366 8517 : Manager::certStore(const std::string& accountId) const
3367 : {
3368 8517 : if (const auto& account = getAccount<JamiAccount>(accountId)) {
3369 17034 : return account->certStore();
3370 8517 : }
3371 0 : throw std::runtime_error("No account found");
3372 : }
3373 :
3374 : } // namespace jami
|