Ring Daemon 16.0.0
Loading...
Searching...
No Matches
call_factory.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include <stdexcept>
19
20#include "call_factory.h"
21#include "sip/sipcall.h"
22#include "sip/sipaccountbase.h"
23#include "string_utils.h"
24
25namespace jami {
26
27// generate something like 7ea037947eb9fb2f
28std::string
30{
31 std::string random_id;
32 do {
33 random_id = std::to_string(
34 std::uniform_int_distribution<uint64_t>(1, JAMI_ID_MAX_VAL)(rand_));
35 } while (hasCall(random_id));
36 return random_id;
37}
38
39std::shared_ptr<SIPCall>
40CallFactory::newSipCall(const std::shared_ptr<SIPAccountBase>& account,
41 Call::CallType type,
42 const std::vector<libjami::MediaMap>& mediaList)
43{
44 if (not allowNewCall_) {
45 JAMI_WARN("Creation of new calls is not allowed");
46 return {};
47 }
48
49 std::lock_guard lk(callMapsMutex_);
50 auto id = getNewCallID();
51 auto call = std::make_shared<SIPCall>(account, id, type, mediaList);
52 callMaps_[call->getLinkType()].emplace(id, call);
53 account->attach(call);
54 return call;
55}
56
57void
59{
60 allowNewCall_ = false;
61}
62
63void
65{
66 std::lock_guard lk(callMapsMutex_);
67
68 const auto& id = call.getCallId();
69 JAMI_DBG("Removing call %s", id.c_str());
70 auto& map = callMaps_.at(call.getLinkType());
71 map.erase(id);
72 JAMI_DBG("Remaining %zu call", map.size());
73}
74
75void
76CallFactory::removeCall(const std::string& id)
77{
78 std::lock_guard lk(callMapsMutex_);
79
80 if (auto call = getCall(id)) {
81 removeCall(*call);
82 } else
83 JAMI_ERR("No call with ID %s", id.c_str());
84}
85
86bool
87CallFactory::hasCall(const std::string& id) const
88{
89 std::lock_guard lk(callMapsMutex_);
90
91 for (const auto& item : callMaps_) {
92 const auto& map = item.second;
93 if (map.find(id) != map.cend())
94 return true;
95 }
96
97 return false;
98}
99
100bool
102{
103 std::lock_guard lk(callMapsMutex_);
104
105 for (const auto& item : callMaps_) {
106 if (not item.second.empty())
107 return false;
108 }
109
110 return true;
111}
112
113void
115{
116 std::lock_guard lk(callMapsMutex_);
117 callMaps_.clear();
118}
119
120std::shared_ptr<Call>
121CallFactory::getCall(const std::string& id) const
122{
123 std::lock_guard lk(callMapsMutex_);
124
125 for (const auto& item : callMaps_) {
126 const auto& map = item.second;
127 const auto& iter = map.find(id);
128 if (iter != map.cend())
129 return iter->second;
130 }
131
132 return nullptr;
133}
134
135std::vector<std::shared_ptr<Call>>
137{
138 std::lock_guard lk(callMapsMutex_);
139 std::vector<std::shared_ptr<Call>> v;
140
141 for (const auto& itemmap : callMaps_) {
142 const auto& map = itemmap.second;
143 v.reserve(v.size() + map.size());
144 for (const auto& item : map)
145 v.push_back(item.second);
146 }
147
148 return v;
149}
150
151std::vector<std::string>
153{
154 std::vector<std::string> v;
155
156 for (const auto& item : callMaps_) {
157 const auto& map = item.second;
158 for (const auto& it : map)
159 v.push_back(it.first);
160 }
161
162 v.shrink_to_fit();
163 return v;
164}
165
166std::size_t
168{
169 std::lock_guard lk(callMapsMutex_);
170 std::size_t count = 0;
171
172 for (const auto& itemmap : callMaps_)
173 count += itemmap.second.size();
174
175 return count;
176}
177
178bool
179CallFactory::hasCall(const std::string& id, Call::LinkType link) const
180{
181 std::lock_guard lk(callMapsMutex_);
182
183 auto const map = getMap_(link);
184 return map and map->find(id) != map->cend();
185}
186
187bool
189{
190 std::lock_guard lk(callMapsMutex_);
191
192 const auto map = getMap_(link);
193 return !map or map->empty();
194}
195
196std::shared_ptr<Call>
197CallFactory::getCall(const std::string& id, Call::LinkType link) const
198{
199 std::lock_guard lk(callMapsMutex_);
200
201 const auto map = getMap_(link);
202 if (!map)
203 return nullptr;
204
205 const auto& it = map->find(id);
206 if (it == map->cend())
207 return nullptr;
208
209 return it->second;
210}
211
212std::vector<std::shared_ptr<Call>>
214{
215 std::lock_guard lk(callMapsMutex_);
216 std::vector<std::shared_ptr<Call>> v;
217
218 const auto map = getMap_(link);
219 if (map) {
220 for (const auto& it : *map)
221 v.push_back(it.second);
222 }
223
224 v.shrink_to_fit();
225 return v;
226}
227
228std::vector<std::string>
230{
231 std::lock_guard lk(callMapsMutex_);
232 std::vector<std::string> v;
233
234 const auto map = getMap_(link);
235 if (map) {
236 for (const auto& it : *map)
237 v.push_back(it.first);
238 }
239
240 v.shrink_to_fit();
241 return v;
242}
243
244std::size_t
246{
247 std::lock_guard lk(callMapsMutex_);
248
249 const auto map = getMap_(link);
250 if (!map)
251 return 0;
252
253 return map->size();
254}
255
256} // 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:126
const std::string & getCallId() const
Return a reference on the call id.
Definition call.h:132
CallType
This determines if the call originated from the local user (OUTGOING) or from some remote peer (INCOM...
Definition call.h:120
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_DBG(...)
Definition logger.h:216
#define JAMI_WARN(...)
Definition logger.h:217
void emitSignal(Args... args)
Definition ring_signal.h:64
static constexpr uint64_t JAMI_ID_MAX_VAL
Definition account.h:62
SIPCall are SIP implementation of a normal Call.