Ring Daemon
Loading...
Searching...
No Matches
jami.h
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#ifndef LIBJAMI_H
18#define LIBJAMI_H
19
20#include "def.h"
21
22#include <functional>
23#include <string>
24#include <map>
25#include <memory>
26#include <filesystem>
27
28#include "trace-tools.h"
29
30namespace libjami {
31
32/* flags for initialization */
45
49LIBJAMI_PUBLIC const char* version() noexcept;
50
54LIBJAMI_PUBLIC std::string_view platform() noexcept;
55
59LIBJAMI_PUBLIC std::string_view arch() noexcept;
60
67LIBJAMI_PUBLIC bool init(enum InitFlag flags) noexcept;
68
74LIBJAMI_PUBLIC bool start(const std::filesystem::path& config_file = {}) noexcept;
75
79LIBJAMI_PUBLIC void fini() noexcept;
80
81LIBJAMI_PUBLIC bool initialized() noexcept;
82
88LIBJAMI_PUBLIC void logging(const std::string& whom, const std::string& action) noexcept;
89
90/* External Callback Dynamic Utilities
91 *
92 * The library provides to users a way to be acknowledged
93 * when daemon's objects have a state change.
94 * The user is aware of this changement when the deamon calls
95 * a user-given callback.
96 * Daemon handles many of these callbacks, one per event type.
97 * The user registers his callbacks using registerXXXXHandlers() functions.
98 * As each callback has its own function signature,
99 * to keep compatibility over releases we don't let user directly provides
100 * his callbacks as it or through a structure.
101 * This way brings ABI violation if we need to change the order
102 * and/or the existence of any callback type.
103 * Thus the user have to pass them using following template classes
104 * and functions, that wraps user-callback in a generic and ABI-compatible way.
105 */
106
107/* Generic class to transit user callbacks to daemon library.
108 * Used conjointly with std::shared_ptr to hide the concrete class.
109 * See CallbackWrapper template for details.
110 */
112{
113protected:
114 // Because post() needs Manager, it should be defined in a .cpp
115 // so not in a templated class.
116 // Also we do not want this method to be public in the API.
117 void post(std::function<void()> cb);
118};
119
120/* Concrete class of CallbackWrapperBase.
121 * This class wraps callbacks of a specific signature.
122 * Also used to obtain the user callback from a CallbackWrapperBase shared ptr.
123 *
124 * This class is CopyConstructible, CopyAssignable, MoveConstructible
125 * and MoveAssignable.
126 */
127template<typename TProto>
129{
130private:
131 using TFunc = std::function<TProto>;
132 TFunc cb_; // The user-callback
133
134public:
135 const char* file_;
136 uint32_t linum_;
137
138 // Empty wrapper: no callback associated.
139 // Used to initialize internal callback arrays.
140 CallbackWrapper() noexcept {}
141
142 // Create and initialize a wrapper to given callback.
143 CallbackWrapper(TFunc&& func, const char* filename, uint32_t linum) noexcept
144 : cb_(std::forward<TFunc>(func))
145 , file_(filename)
146 , linum_(linum)
147 {}
148
149 // Create and initialize a wrapper from a generic CallbackWrapperBase
150 // shared pointer.
151 // Note: the given callback is copied into internal storage.
152 CallbackWrapper(const std::shared_ptr<CallbackWrapperBase>& p) noexcept
153 {
154 if (p) {
155 auto other = (CallbackWrapper<TProto>*) p.get();
156
157 cb_ = other->cb_;
158 file_ = other->file_;
159 linum_ = other->linum_;
160 }
161 }
162
163 // Return user-callback reference.
164 // The returned std::function can be null-initialized if no callback
165 // has been set.
166 constexpr const TFunc& operator*() const noexcept { return cb_; }
167
168 // Return boolean true value if a non-null callback has been set
169 constexpr explicit operator bool() const noexcept { return static_cast<bool>(cb_); }
170};
171
172/* Concrete class of CallbackWrapperBase.
173 * This class wraps callbacks of a specific signature.
174 * Used to retrigger callbacks on a io context to avoid lock if signals cannot
175 * be emitted while a method is called.
176 * Also used to obtain the user callback from a CallbackWrapperBase shared ptr.
177 *
178 * This class is CopyConstructible, CopyAssignable, MoveConstructible
179 * and MoveAssignable.
180 */
181template<typename TProto>
183{
184private:
185 using TFunc = std::function<TProto>;
186 TFunc cb_; // The user-callback
187
188 // This is quite a ugly method used to transmit templated TFunc with their arguments in the
189 // ioContext of the manager to avoid locks for signals.
190 template<typename TCallback>
191 auto ioContextWrapper(TCallback&& fun)
192 {
193 return [this, fun {std::forward<TCallback>(fun)}](
194 auto&&... args) -> decltype(fun(std::forward<decltype(args)>(args)...)) {
195 post([fun {std::move(fun)}, forwardArgs = std::make_tuple(std::move(args)...)]() mutable {
196 std::apply(std::move(fun), std::move(forwardArgs));
197 });
198 };
199 }
200
201public:
202 const char* file_;
203 uint32_t linum_;
204
205 // Empty wrapper: no callback associated.
206 // Used to initialize internal callback arrays.
208
209 // Create and initialize a wrapper to given callback.
210 SerializedCallbackWrapper(TFunc&& func, const char* filename, uint32_t linum) noexcept
211 : file_(filename)
212 , linum_(linum)
213 {
214 cb_ = ioContextWrapper(func);
215 }
216
217 // Create and initialize a wrapper from a generic CallbackWrapperBase
218 // shared pointer.
219 // Note: the given callback is copied into internal storage.
220 SerializedCallbackWrapper(const std::shared_ptr<CallbackWrapperBase>& p) noexcept
221 {
222 if (p) {
223 auto other = (CallbackWrapper<TProto>*) p.get();
224
225 cb_ = ioContextWrapper(other.cb_);
226 file_ = other->file_;
227 linum_ = other->linum_;
228 }
229 }
230
231 // Return user-callback reference.
232 // The returned std::function can be null-initialized if no callback
233 // has been set.
234 constexpr const TFunc& operator*() const noexcept { return cb_; }
235
236 // Return boolean true value if a non-null callback has been set
237 constexpr explicit operator bool() const noexcept { return static_cast<bool>(cb_); }
238};
239
246template<typename Ts>
247std::pair<std::string, std::shared_ptr<CallbackWrapperBase>>
248exportable_callback(std::function<typename Ts::cb_type>&& func,
249 const char* file = CURRENT_FILENAME(),
250 uint32_t linum = CURRENT_LINE())
251{
252 return std::make_pair((const std::string&) Ts::name,
254 std::forward<std::function<typename Ts::cb_type>>(func), file, linum));
255}
256
257template<typename Ts>
258std::pair<std::string, std::shared_ptr<CallbackWrapperBase>>
259exportable_serialized_callback(std::function<typename Ts::cb_type>&& func,
260 const char* file = CURRENT_FILENAME(),
261 uint32_t linum = CURRENT_LINE())
262{
263 return std::make_pair((const std::string&) Ts::name,
265 std::forward<std::function<typename Ts::cb_type>>(func), file, linum));
266}
267
268LIBJAMI_PUBLIC void registerSignalHandlers(const std::map<std::string, std::shared_ptr<CallbackWrapperBase>>&);
270
271using MediaMap = std::map<std::string, std::string>;
272
273} // namespace libjami
274
275#endif /* LIBJAMI_H */
void post(std::function< void()> cb)
Definition jami.cpp:144
constexpr const TFunc & operator*() const noexcept
Definition jami.h:166
CallbackWrapper(const std::shared_ptr< CallbackWrapperBase > &p) noexcept
Definition jami.h:152
CallbackWrapper(TFunc &&func, const char *filename, uint32_t linum) noexcept
Definition jami.h:143
CallbackWrapper() noexcept
Definition jami.h:140
const char * file_
Definition jami.h:135
SerializedCallbackWrapper(TFunc &&func, const char *filename, uint32_t linum) noexcept
Definition jami.h:210
constexpr const TFunc & operator*() const noexcept
Definition jami.h:234
SerializedCallbackWrapper(const std::shared_ptr< CallbackWrapperBase > &p) noexcept
Definition jami.h:220
#define LIBJAMI_PUBLIC
Definition def.h:42
const char * version() noexcept
Return the library version as string.
Definition buildinfo.cpp:40
InitFlag
Definition jami.h:33
@ LIBJAMI_FLAG_CONSOLE_LOG
Definition jami.h:35
@ LIBJAMI_FLAG_IOS_EXTENSION
Definition jami.h:38
@ LIBJAMI_FLAG_NO_AUTOLOAD
Definition jami.h:43
@ LIBJAMI_FLAG_SYSLOG
Definition jami.h:36
@ LIBJAMI_FLAG_AUTOANSWER
Definition jami.h:37
@ LIBJAMI_FLAG_NO_AUTOSYNC
Definition jami.h:42
@ LIBJAMI_FLAG_NO_LOCAL_AUDIO
Definition jami.h:39
@ LIBJAMI_FLAG_NO_LOCAL_VIDEO
Definition jami.h:40
@ LIBJAMI_FLAG_DEBUG
Definition jami.h:34
@ LIBJAMI_FLAG_NO_LOCAL_MEDIA
Definition jami.h:41
void logging(const std::string &whom, const std::string &action) noexcept
Control log handlers.
Definition jami.cpp:128
bool initialized() noexcept
Definition jami.cpp:115
void fini() noexcept
Stop and freeing any resource allocated by daemon.
Definition jami.cpp:121
bool init(enum InitFlag flags) noexcept
Initialize globals, create underlaying daemon.
Definition jami.cpp:35
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:48
std::string_view arch() noexcept
Return the target architecture as a string.
Definition buildinfo.cpp:54
void unregisterSignalHandlers()
std::map< std::string, std::string > MediaMap
Definition jami.h:271
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:259
bool start(const std::filesystem::path &config_file) noexcept
Start asynchronously daemon created by init().
Definition jami.cpp:104
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:248
#define CURRENT_LINE()
Definition trace-tools.h:28
#define CURRENT_FILENAME()
Definition trace-tools.h:27