Ring Daemon
Loading...
Searching...
No Matches
call_factory.cpp
Go to the documentation of this file.
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#include "call_factory.h"
19#include "sip/sipcall.h"
20#include "sip/sipaccountbase.h"
21
22namespace jami {
23
24// generate something like 7ea037947eb9fb2f
25std::string
27{
28 std::string random_id;
29 do {
30 random_id = std::to_string(std::uniform_int_distribution<uint64_t>(1, JAMI_ID_MAX_VAL)(rand_));
31 } while (hasCall(random_id));
32 return random_id;
33}
34
35std::shared_ptr<SIPCall>
36CallFactory::newSipCall(const std::shared_ptr<SIPAccountBase>& account,
37 Call::CallType type,
38 const std::vector<libjami::MediaMap>& mediaList)
39{
40 if (not allowNewCall_) {
41 JAMI_WARN("Creation of new calls is not allowed");
42 return {};
43 }
44
45 std::lock_guard lk(callMapsMutex_);
46 auto id = getNewCallID();
47 auto call = std::make_shared<SIPCall>(account, id, type, mediaList);
48 callMaps_[call->getLinkType()].emplace(id, call);
49 account->attach(call);
50 return call;
51}
52
53void
55{
56 allowNewCall_ = false;
57}
58
59void
61{
62 std::lock_guard lk(callMapsMutex_);
63
64 const auto& id = call.getCallId();
65 JAMI_DBG("Removing call %s", id.c_str());
66 auto& map = callMaps_.at(call.getLinkType());
67 map.erase(id);
68 JAMI_DBG("Remaining %zu call", map.size());
69}
70
71void
72CallFactory::removeCall(const std::string& id)
73{
74 std::lock_guard lk(callMapsMutex_);
75
76 if (auto call = getCall(id)) {
77 removeCall(*call);
78 } else
79 JAMI_ERR("No call with ID %s", id.c_str());
80}
81
82bool
83CallFactory::hasCall(const std::string& id) const
84{
85 std::lock_guard lk(callMapsMutex_);
86
87 for (const auto& item : callMaps_) {
88 const auto& map = item.second;
89 if (map.find(id) != map.cend())
90 return true;
91 }
92
93 return false;
94}
95
96bool
98{
99 std::lock_guard lk(callMapsMutex_);
100
101 for (const auto& item : callMaps_) {
102 if (not item.second.empty())
103 return false;
104 }
105
106 return true;
107}
108
109void
111{
112 std::lock_guard lk(callMapsMutex_);
113 callMaps_.clear();
114}
115
116std::shared_ptr<Call>
117CallFactory::getCall(const std::string& id) const
118{
119 std::lock_guard lk(callMapsMutex_);
120
121 for (const auto& item : callMaps_) {
122 const auto& map = item.second;
123 const auto& iter = map.find(id);
124 if (iter != map.cend())
125 return iter->second;
126 }
127
128 return nullptr;
129}
130
131std::vector<std::shared_ptr<Call>>
133{
134 std::lock_guard lk(callMapsMutex_);
135 std::vector<std::shared_ptr<Call>> v;
136
137 for (const auto& itemmap : callMaps_) {
138 const auto& map = itemmap.second;
139 v.reserve(v.size() + map.size());
140 for (const auto& item : map)
141 v.push_back(item.second);
142 }
143
144 return v;
145}
146
147std::vector<std::string>
149{
150 std::vector<std::string> v;
151
152 for (const auto& item : callMaps_) {
153 const auto& map = item.second;
154 for (const auto& it : map)
155 v.push_back(it.first);
156 }
157
158 v.shrink_to_fit();
159 return v;
160}
161
162std::size_t
164{
165 std::lock_guard lk(callMapsMutex_);
166 std::size_t count = 0;
167
168 for (const auto& itemmap : callMaps_)
169 count += itemmap.second.size();
170
171 return count;
172}
173
174bool
175CallFactory::hasCall(const std::string& id, Call::LinkType link) const
176{
177 std::lock_guard lk(callMapsMutex_);
178
179 const auto* const map = getMap_(link);
180 return map and map->find(id) != map->cend();
181}
182
183bool
185{
186 std::lock_guard lk(callMapsMutex_);
187
188 const auto* const map = getMap_(link);
189 return !map or map->empty();
190}
191
192std::shared_ptr<Call>
193CallFactory::getCall(const std::string& id, Call::LinkType link) const
194{
195 std::lock_guard lk(callMapsMutex_);
196
197 const auto* const map = getMap_(link);
198 if (!map)
199 return nullptr;
200
201 const auto& it = map->find(id);
202 if (it == map->cend())
203 return nullptr;
204
205 return it->second;
206}
207
208std::vector<std::shared_ptr<Call>>
210{
211 std::lock_guard lk(callMapsMutex_);
212 std::vector<std::shared_ptr<Call>> v;
213
214 const auto* const map = getMap_(link);
215 if (map) {
216 for (const auto& it : *map)
217 v.push_back(it.second);
218 }
219
220 v.shrink_to_fit();
221 return v;
222}
223
224std::vector<std::string>
226{
227 std::lock_guard lk(callMapsMutex_);
228 std::vector<std::string> v;
229
230 const auto* const map = getMap_(link);
231 if (map) {
232 for (const auto& it : *map)
233 v.push_back(it.first);
234 }
235
236 v.shrink_to_fit();
237 return v;
238}
239
240std::size_t
242{
243 std::lock_guard lk(callMapsMutex_);
244
245 const auto* const map = getMap_(link);
246 if (!map)
247 return 0;
248
249 return map->size();
250}
251
252} // namespace jami
std::size_t callCount() const
Return number of calls.
std::vector< std::shared_ptr< Call > > getAllCalls() const
Return all calls.
std::shared_ptr< SIPCall > newSipCall(const std::shared_ptr< SIPAccountBase > &account, Call::CallType type, const std::vector< libjami::MediaMap > &mediaList)
Create a new call instance.
void forbid()
Forbid creation of new calls.
std::vector< std::string > getCallIDs() const
Return all call's IDs.
bool hasCall(const std::string &id) const
Return if given call exists.
void removeCall(Call &call)
Remove given call instance from call list.
std::shared_ptr< Call > getCall(const std::string &id) const
Return call pointer associated to given ID.Type can optionally be specified.
std::string getNewCallID() const
void clear()
Erase all calls.
bool empty() const
Return if calls exist.
virtual LinkType getLinkType() const
Definition call.h:105
const std::string & getCallId() const
Return a reference on the call id.
Definition call.h:111
CallType
This determines if the call originated from the local user (OUTGOING) or from some remote peer (INCOM...
Definition call.h:101
#define JAMI_ERR(...)
Definition logger.h:230
#define JAMI_DBG(...)
Definition logger.h:228
#define JAMI_WARN(...)
Definition logger.h:229
void emitSignal(Args... args)
Definition jami_signal.h:64
static constexpr uint64_t JAMI_ID_MAX_VAL
Definition account.h:57
SIPCall are SIP implementation of a normal Call.