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 : #pragma once
18 :
19 : #include "call.h"
20 : #include "conference.h"
21 :
22 : #include <map>
23 : #include <memory>
24 : #include <string>
25 : #include <mutex>
26 :
27 : namespace jami {
28 :
29 : class CallSet
30 : {
31 : public:
32 648 : std::shared_ptr<Call> getCall(const std::string& callId) const
33 : {
34 648 : std::lock_guard l(mutex_);
35 648 : auto i = calls_.find(callId);
36 1296 : return i == calls_.end() ? std::shared_ptr<Call> {} : i->second.lock();
37 648 : }
38 97 : std::shared_ptr<Conference> getConference(const std::string& conferenceId) const
39 : {
40 97 : std::lock_guard l(mutex_);
41 97 : auto i = conferences_.find(conferenceId);
42 194 : return i == conferences_.end() ? std::shared_ptr<Conference> {} : i->second;
43 97 : }
44 :
45 371 : void add(const std::shared_ptr<Call>& call)
46 : {
47 371 : std::lock_guard l(mutex_);
48 371 : calls_.emplace(call->getCallId(), call);
49 371 : }
50 37 : void add(const std::shared_ptr<Conference>& conference)
51 : {
52 37 : std::lock_guard l(mutex_);
53 37 : conferences_.emplace(conference->getConfId(), conference);
54 37 : }
55 385 : bool remove(const std::shared_ptr<Call>& call)
56 : {
57 385 : std::lock_guard l(mutex_);
58 770 : return calls_.erase(call->getCallId()) > 0;
59 385 : }
60 56 : bool removeConference(const std::string& confId)
61 : {
62 56 : std::lock_guard l(mutex_);
63 112 : return conferences_.erase(confId) > 0;
64 56 : }
65 :
66 773 : std::vector<std::string> getCallIds() const
67 : {
68 773 : std::lock_guard l(mutex_);
69 773 : std::vector<std::string> ids;
70 773 : ids.reserve(calls_.size());
71 795 : for (const auto& callIt : calls_)
72 22 : ids.emplace_back(callIt.first);
73 1546 : return ids;
74 773 : }
75 : std::vector<std::shared_ptr<Call>> getCalls() const
76 : {
77 : std::lock_guard l(mutex_);
78 : std::vector<std::shared_ptr<Call>> calls;
79 : calls.reserve(calls_.size());
80 : for (const auto& callIt : calls_)
81 : if (auto call = callIt.second.lock())
82 : calls.emplace_back(std::move(call));
83 : return calls;
84 : }
85 :
86 4 : std::vector<std::string> getConferenceIds() const
87 : {
88 4 : std::lock_guard l(mutex_);
89 4 : std::vector<std::string> ids;
90 4 : ids.reserve(conferences_.size());
91 6 : for (const auto& confIt : conferences_)
92 2 : ids.emplace_back(confIt.first);
93 8 : return ids;
94 4 : }
95 : std::vector<std::shared_ptr<Conference>> getConferences() const
96 : {
97 : std::lock_guard l(mutex_);
98 : std::vector<std::shared_ptr<Conference>> confs;
99 : confs.reserve(conferences_.size());
100 : for (const auto& confIt : conferences_)
101 : if (const auto& conf = confIt.second)
102 : confs.emplace_back(conf);
103 : return confs;
104 : }
105 :
106 : private:
107 : mutable std::mutex mutex_;
108 : std::map<std::string, std::weak_ptr<Call>> calls_;
109 : std::map<std::string, std::shared_ptr<Conference>> conferences_;
110 : };
111 :
112 : } // namespace jami
|