Ring Daemon 16.0.0
Loading...
Searching...
No Matches
jami.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#ifndef LIBJAMI_H
18#define LIBJAMI_H
19
20#include "def.h"
21
22#include <vector>
23#include <functional>
24#include <string>
25#include <map>
26#include <memory>
27#include <type_traits>
28#include <filesystem>
29
30#include "trace-tools.h"
31
32namespace libjami {
33
34/* flags for initialization */
47
51LIBJAMI_PUBLIC const char* version() noexcept;
52
56LIBJAMI_PUBLIC std::string_view platform() noexcept;
57
61LIBJAMI_PUBLIC std::string_view arch() noexcept;
62
69LIBJAMI_PUBLIC bool init(enum InitFlag flags) noexcept;
70
76LIBJAMI_PUBLIC bool start(const std::filesystem::path& config_file = {}) noexcept;
77
81LIBJAMI_PUBLIC void fini() noexcept;
82
83LIBJAMI_PUBLIC bool initialized() noexcept;
84
90LIBJAMI_PUBLIC void logging(const std::string& whom, const std::string& action) noexcept;
91
92/* External Callback Dynamic Utilities
93 *
94 * The library provides to users a way to be acknowledged
95 * when daemon's objects have a state change.
96 * The user is aware of this changement when the deamon calls
97 * a user-given callback.
98 * Daemon handles many of these callbacks, one per event type.
99 * The user registers his callbacks using registerXXXXHandlers() functions.
100 * As each callback has its own function signature,
101 * to keep compatibility over releases we don't let user directly provides
102 * his callbacks as it or through a structure.
103 * This way brings ABI violation if we need to change the order
104 * and/or the existence of any callback type.
105 * Thus the user have to pass them using following template classes
106 * and functions, that wraps user-callback in a generic and ABI-compatible way.
107 */
108
109/* Generic class to transit user callbacks to daemon library.
110 * Used conjointly with std::shared_ptr to hide the concrete class.
111 * See CallbackWrapper template for details.
112 */
114{
115protected:
116 // Because post() needs Manager, it should be defined in a .cpp
117 // so not in a templated class.
118 // Also we do not want this method to be public in the API.
119 void post(std::function<void()> cb);
120};
121
122/* Concrete class of CallbackWrapperBase.
123 * This class wraps callbacks of a specific signature.
124 * Also used to obtain the user callback from a CallbackWrapperBase shared ptr.
125 *
126 * This class is CopyConstructible, CopyAssignable, MoveConstructible
127 * and MoveAssignable.
128 */
129template<typename TProto>
131{
132private:
133 using TFunc = std::function<TProto>;
134 TFunc cb_; // The user-callback
135
136public:
137 const char* file_;
138 uint32_t linum_;
139
140 // Empty wrapper: no callback associated.
141 // Used to initialize internal callback arrays.
142 CallbackWrapper() noexcept {}
143
144 // Create and initialize a wrapper to given callback.
145 CallbackWrapper(TFunc&& func, const char* filename, uint32_t linum) noexcept
146 : cb_(std::forward<TFunc>(func))
147 , file_(filename)
148 , linum_(linum)
149 {}
150
151 // Create and initialize a wrapper from a generic CallbackWrapperBase
152 // shared pointer.
153 // Note: the given callback is copied into internal storage.
154 CallbackWrapper(const std::shared_ptr<CallbackWrapperBase>& p) noexcept
155 {
156 if (p) {
157 auto other = (CallbackWrapper<TProto>*) p.get();
158
159 cb_ = other->cb_;
160 file_ = other->file_;
161 linum_ = other->linum_;
162 }
163 }
164
165 // Return user-callback reference.
166 // The returned std::function can be null-initialized if no callback
167 // has been set.
168 constexpr const TFunc& operator*() const noexcept { return cb_; }
169
170 // Return boolean true value if a non-null callback has been set
171 constexpr explicit operator bool() const noexcept { return static_cast<bool>(cb_); }
172};
173
174/* Concrete class of CallbackWrapperBase.
175 * This class wraps callbacks of a specific signature.
176 * Used to retrigger callbacks on a io context to avoid lock if signals cannot
177 * be emitted while a method is called.
178 * Also used to obtain the user callback from a CallbackWrapperBase shared ptr.
179 *
180 * This class is CopyConstructible, CopyAssignable, MoveConstructible
181 * and MoveAssignable.
182 */
183template<typename TProto>
185{
186private:
187 using TFunc = std::function<TProto>;
188 TFunc cb_; // The user-callback
189
190 // This is quite a ugly method used to transmit templated TFunc with their arguments in the
191 // ioContext of the manager to avoid locks for signals.
192 template<typename TCallback>
193 auto ioContextWrapper(TCallback&& fun)
194 {
195 return [this, fun {std::move(fun)}](
196 auto&&... args) -> decltype(fun(std::forward<decltype(args)>(args)...)) {
197 post([fun {std::move(fun)},
198 forwardArgs = std::make_tuple(std::move(args)...)]() mutable {
199 std::apply(std::move(fun), std::move(forwardArgs));
200 });
201 };
202 }
203
204public:
205 const char* file_;
206 uint32_t linum_;
207
208 // Empty wrapper: no callback associated.
209 // Used to initialize internal callback arrays.
211
212 // Create and initialize a wrapper to given callback.
213 SerializedCallbackWrapper(TFunc&& func, const char* filename, uint32_t linum) noexcept
214 : file_(filename)
215 , linum_(linum)
216 {
217 cb_ = ioContextWrapper(func);
218 }
219
220 // Create and initialize a wrapper from a generic CallbackWrapperBase
221 // shared pointer.
222 // Note: the given callback is copied into internal storage.
223 SerializedCallbackWrapper(const std::shared_ptr<CallbackWrapperBase>& p) noexcept
224 {
225 if (p) {
226 auto other = (CallbackWrapper<TProto>*) p.get();
227
228 cb_ = ioContextWrapper(other.cb_);
229 file_ = other->file_;
230 linum_ = other->linum_;
231 }
232 }
233
234 // Return user-callback reference.
235 // The returned std::function can be null-initialized if no callback
236 // has been set.
237 constexpr const TFunc& operator*() const noexcept { return cb_; }
238
239 // Return boolean true value if a non-null callback has been set
240 constexpr explicit operator bool() const noexcept { return static_cast<bool>(cb_); }
241};
242
249template<typename Ts>
250std::pair<std::string, std::shared_ptr<CallbackWrapperBase>>
251exportable_callback(std::function<typename Ts::cb_type>&& func,
252 const char* file = CURRENT_FILENAME(),
253 uint32_t linum = CURRENT_LINE())
254{
255 return std::make_pair((const std::string&) Ts::name,
257 std::forward<std::function<typename Ts::cb_type>>(func), file, linum));
258}
259
260template<typename Ts>
261std::pair<std::string, std::shared_ptr<CallbackWrapperBase>>
262exportable_serialized_callback(std::function<typename Ts::cb_type>&& func,
263 const char* file = CURRENT_FILENAME(),
264 uint32_t linum = CURRENT_LINE())
265{
266 return std::make_pair((const std::string&) Ts::name,
268 std::forward<std::function<typename Ts::cb_type>>(func), file, linum));
269}
270
272 const std::map<std::string, std::shared_ptr<CallbackWrapperBase>>&);
274
275using MediaMap = std::map<std::string, std::string>;
276
277} // namespace libjami
278
279#endif /* LIBJAMI_H */
void post(std::function< void()> cb)
Definition ring_api.cpp:121
constexpr const TFunc & operator*() const noexcept
Definition jami.h:168
CallbackWrapper(const std::shared_ptr< CallbackWrapperBase > &p) noexcept
Definition jami.h:154
CallbackWrapper(TFunc &&func, const char *filename, uint32_t linum) noexcept
Definition jami.h:145
CallbackWrapper() noexcept
Definition jami.h:142
const char * file_
Definition jami.h:137
SerializedCallbackWrapper(TFunc &&func, const char *filename, uint32_t linum) noexcept
Definition jami.h:213
constexpr const TFunc & operator*() const noexcept
Definition jami.h:237
SerializedCallbackWrapper(const std::shared_ptr< CallbackWrapperBase > &p) noexcept
Definition jami.h:223
#define LIBJAMI_PUBLIC
Definition def.h:42
LIBJAMI_PUBLIC bool start(const std::filesystem::path &config_file={}) noexcept
Start asynchronously daemon created by init().
Definition ring_api.cpp:81
const char * version() noexcept
Return the library version as string.
Definition buildinfo.cpp:43
LIBJAMI_PUBLIC void logging(const std::string &whom, const std::string &action) noexcept
Control log handlers.
Definition ring_api.cpp:105
void registerSignalHandlers(const std::map< std::string, std::shared_ptr< CallbackWrapperBase > > &handlers)
std::string_view platform() noexcept
Return the target platform (OS) as a string.
Definition buildinfo.cpp:51
LIBJAMI_PUBLIC bool init(enum InitFlag flags) noexcept
Initialize globals, create underlaying daemon.
Definition ring_api.cpp:44
std::string_view arch() noexcept
Return the target architecture as a string.
Definition buildinfo.cpp:57
void unregisterSignalHandlers()
std::map< std::string, std::string > MediaMap
Definition jami.h:275
LIBJAMI_PUBLIC bool initialized() noexcept
Definition ring_api.cpp:92
std::pair< std::string, std::shared_ptr< CallbackWrapperBase > > exportable_serialized_callback(std::function< typename Ts::cb_type > &&func, const char *file=CURRENT_FILENAME(), uint32_t linum=CURRENT_LINE())
Definition jami.h:262
LIBJAMI_PUBLIC void fini() noexcept
Stop and freeing any resource allocated by daemon.
Definition ring_api.cpp:98
InitFlag
Definition jami.h:35
@ LIBJAMI_FLAG_CONSOLE_LOG
Definition jami.h:37
@ LIBJAMI_FLAG_IOS_EXTENSION
Definition jami.h:40
@ LIBJAMI_FLAG_NO_AUTOLOAD
Definition jami.h:45
@ LIBJAMI_FLAG_SYSLOG
Definition jami.h:38
@ LIBJAMI_FLAG_AUTOANSWER
Definition jami.h:39
@ LIBJAMI_FLAG_NO_AUTOSYNC
Definition jami.h:44
@ LIBJAMI_FLAG_NO_LOCAL_AUDIO
Definition jami.h:41
@ LIBJAMI_FLAG_NO_LOCAL_VIDEO
Definition jami.h:42
@ LIBJAMI_FLAG_DEBUG
Definition jami.h:36
@ LIBJAMI_FLAG_NO_LOCAL_MEDIA
Definition jami.h:43
std::pair< std::string, std::shared_ptr< CallbackWrapperBase > > exportable_callback(std::function< typename Ts::cb_type > &&func, const char *file=CURRENT_FILENAME(), uint32_t linum=CURRENT_LINE())
Return an exportable callback object.
Definition jami.h:251
#define CURRENT_LINE()
Definition trace-tools.h:28
#define CURRENT_FILENAME()
Definition trace-tools.h:27