UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
lib_common.cpp File Reference
#include <iostream>
#include <map>
#include "debug.h"
#include "host.h"
#include "lib_common.h"
#include "utils/color_out.h"
Include dependency graph for lib_common.cpp:

Go to the source code of this file.

Classes

struct  ci_less
 
struct  ci_less::nocase_compare
 
struct  lib_info
 
struct  library_class_info_t
 

Macros

#define MOD_NAME   "[lib] "
 

Functions

static auto & get_libmap ()
 
map< string, const void * > get_libraries_for_class (enum library_class cls, int abi_version, bool include_hidden)
 
bool list_all_modules ()
 
void list_modules (enum library_class cls, int abi_version, bool full)
 
const void * load_library (const char *name, enum library_class cls, int abi_version)
 
void open_all (const char *pattern, list< void * > &libs)
 
void register_library (const char *name, const void *data, enum library_class cls, int abi_version, int hidden)
 

Variables

static map< string, string > lib_errors
 
const map< enum library_class, library_class_info_t > library_class_info
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file lib_common.cpp.

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[lib] "

Definition at line 58 of file lib_common.cpp.

Function Documentation

◆ get_libmap()

static auto & get_libmap ( )
static

Definition at line 242 of file lib_common.cpp.

242 {
243 /* This is needed because register_library() may be called before global
244 * static members are initialized (it is __attribute__((constructor)))
245 */
246 static map<enum library_class, map<string, lib_info, ci_less>> libraries;
247 return libraries;
248}

Referenced by get_libraries_for_class(), list_all_modules(), load_library(), and register_library().

◆ get_libraries_for_class()

map< string, const void * > get_libraries_for_class ( enum library_class cls,
int abi_version,
bool include_hidden )

Definition at line 336 of file lib_common.cpp.

337{
338 map<string, const void *> ret;
339 auto& libraries = get_libmap();
340 auto it = libraries.find(cls);
341 if (it != libraries.end()) {
342 for (auto && item : it->second) {
343 if (abi_version == item.second.abi_version) {
344 if (include_hidden || !item.second.hidden) {
345 ret[item.first] = item.second.data;
346 }
347 } else {
348 LOG(LOG_LEVEL_WARNING) << "Module " << item.first << " ABI version mismatch (required " <<
349 abi_version << ", have " << item.second.abi_version << ")\n";
350
351 }
352 }
353 }
354
355 return ret;
356}
#define LOG(level)
Definition debug.h:313
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
enum library_class cls
Definition host.cpp:749
static auto & get_libmap()
Definition hd-rum.c:27

References cls, get_libmap(), LOG, and LOG_LEVEL_WARNING.

Referenced by audio_codec_init_real(), audio_filter_init(), create_filter(), find_best_decompress(), list_modules(), and print_capabilities().

Here is the call graph for this function:

◆ list_all_modules()

bool list_all_modules ( )
Return values
falseif there occurs some problem opening one or more modules, true otherwise

Definition at line 308 of file lib_common.cpp.

308 {
309 bool ret = true;
310
311 auto& libraries = get_libmap();
312 for (auto cls_it = library_class_info.begin(); cls_it != library_class_info.end();
313 ++cls_it) {
314 cout << cls_it->second.class_name << "\n";
315 auto it = libraries.find(cls_it->first);
316 if (it != libraries.end()) {
317 for (auto && item : it->second) {
318 col() << "\t" << SBOLD(item.first) << "\n";
319 }
320 }
321 cout << "\n";
322 }
323
324 if (!lib_errors.empty()) {
325 ret = false;
326 col() << SBOLD(SRED("Errors:")) << "\n";
327 for (auto && item : lib_errors) {
328 cout << "\t" << SRED(item.first) << "\n\t\t" << item.second << "\n";
329 }
330 cout << "\n";
331 }
332
333 return ret;
334}
#define SRED(x)
Definition color_out.h:85
#define SBOLD(x)
Definition color_out.h:80
const map< enum library_class, library_class_info_t > library_class_info
static map< string, string > lib_errors

References get_libmap(), lib_errors, library_class_info, SBOLD, and SRED.

Referenced by parse_global_opts(), and parse_options_internal().

Here is the call graph for this function:

◆ list_modules()

void list_modules ( enum library_class cls,
int abi_version,
bool full )

Prints list of modules of given class

Parameters
fullinclude hidden modules

Definition at line 298 of file lib_common.cpp.

298 {
299 const auto & class_set = get_libraries_for_class(cls, abi_version, full);
300 for (auto && item : class_set) {
301 col() << "\t" << SBOLD(item.first.c_str()) << "\n";
302 }
303}
map< string, const void * > get_libraries_for_class(enum library_class cls, int abi_version, bool include_hidden)

References cls, get_libraries_for_class(), and SBOLD.

Referenced by audio_capture_print_help(), audio_playback_help(), capture_filter_init(), video_rxtx::list(), list_video_capture_devices(), list_video_display_devices(), show_compress_help(), and show_vo_postprocess_help().

Here is the call graph for this function:

◆ load_library()

const void * load_library ( const char * name,
enum library_class cls,
int abi_version )

Definition at line 259 of file lib_common.cpp.

260{
261 auto it_cls = get_libmap().find(cls);
262 if (it_cls != get_libmap().end()) {
263 auto it_module = it_cls->second.find(name);
264 if (it_module != it_cls->second.end()) {
265 const auto& mod_pair = it_module->second;
266 if (mod_pair.abi_version == abi_version) {
267 return mod_pair.data;
268 } else {
269 LOG(LOG_LEVEL_WARNING) << "Module " << name << " ABI version mismatch (required " <<
270 abi_version << ", have " << mod_pair.abi_version << ")\n";
271 }
272 }
273 }
274
275 // Library was not found or was not loaded due to unsatisfied
276 // dependencies. If the latter one, display reason why dlopen() failed.
277 if (library_class_info.find(cls) != library_class_info.end()) {
278 string filename = "ultragrid_";
279 if (strlen(library_class_info.at(cls).file_prefix) > 0) {
280 filename += library_class_info.at(cls).file_prefix;
281 filename += "_";
282 }
283
284 filename += name + string(".so");
285
286 if (lib_errors.find(filename) != lib_errors.end()) {
287 LOG(LOG_LEVEL_WARNING) << filename << ": " << lib_errors.find(filename)->second << "\n";
288 }
289 }
290
291 return NULL;
292}
const char * name
Definition settings.cpp:193

References cls, get_libmap(), lib_errors, library_class_info, LOG, LOG_LEVEL_WARNING, and name.

Referenced by adjust_params(), audio_capture_init(), audio_decoder_init(), audio_playback_init(), video_rxtx::create(), video_rxtx::get_long_name(), init(), ldgm::init(), initialize_video_capture(), initialize_video_display(), tx_init(), and video_decoder_init().

Here is the call graph for this function:

◆ open_all()

void open_all ( const char * pattern,
list< void * > & libs )

Definition at line 176 of file lib_common.cpp.

176 {
177#ifdef BUILD_LIBRARIES
178 char path[512];
179 glob_t glob_buf;
180
181 /* binary not from $PATH */
182 if (!running_from_path(uv_argv)) {
183 char *tmp = strdup(uv_argv[0]);
184 char *dir = dirname(tmp);
185 snprintf(path, sizeof(path), "%s/../lib/ultragrid/%s", dir, pattern);
186 free(tmp);
187 } else {
188 snprintf(path, sizeof(path), LIB_DIR "/ultragrid/%s", pattern);
189 }
190
191 glob(path, 0, NULL, &glob_buf);
192
193 for(unsigned int i = 0; i < glob_buf.gl_pathc; ++i) {
194 void *handle = dlopen(glob_buf.gl_pathv[i], RTLD_NOW|RTLD_GLOBAL);
195 if (!handle) {
196 char *error = dlerror();
197 MSG(WARNING, "Library %s opening warning: %s \n",
198 glob_buf.gl_pathv[i], error);
199 char *filename = basename(glob_buf.gl_pathv[i]);
200 if (filename && error) {
201 lib_errors.emplace(filename, error);
202 }
203 continue;
204 }
205 libs.push_back(handle);
206 }
207
208 globfree(&glob_buf);
209#else
210 UNUSED(libs);
212#endif
213}
#define UNUSED(x)
pattern
Definition testcard.c:94
#define MSG(l, fmt,...)
Definition debug.h:96
char ** uv_argv
Definition host.cpp:174

References lib_errors, MSG, UNUSED, and uv_argv.

Referenced by common_preinit().

◆ register_library()

void register_library ( const char * name,
const void * data,
enum library_class cls,
int abi_version,
int hidden )

Definition at line 250 of file lib_common.cpp.

251{
252 auto& map = get_libmap()[cls];
253 if (map.find(name) != map.end()) {
254 LOG(LOG_LEVEL_ERROR) << "Module \"" << name << "\" (class " << cls << ") multiple initialization!\n";
255 }
256 map[name] = {data, abi_version, static_cast<bool>(hidden)};
257}
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59

References cls, get_libmap(), LOG, LOG_LEVEL_ERROR, and name.

Here is the call graph for this function:

Variable Documentation

◆ lib_errors

map<string, string> lib_errors
static

Definition at line 81 of file lib_common.cpp.

Referenced by list_all_modules(), load_library(), and open_all().

◆ library_class_info

const map<enum library_class, library_class_info_t> library_class_info
Initial value:
= {
{ LIBRARY_CLASS_UNDEFINED, { "General module", "" }},
{ LIBRARY_CLASS_CAPTURE_FILTER, { "Video capture filter" , "vcapfilter" }},
{ LIBRARY_CLASS_AUDIO_CAPTURE, { "Audio capture device", "acap" }},
{ LIBRARY_CLASS_AUDIO_PLAYBACK, { "Audio playback device", "aplay" }},
{ LIBRARY_CLASS_VIDEO_CAPTURE, { "Video capture device", "vidcap" }},
{ LIBRARY_CLASS_VIDEO_DISPLAY, { "Video display device", "display" }},
{ LIBRARY_CLASS_AUDIO_COMPRESS, { "Audio compression", "acompress" }},
{ LIBRARY_CLASS_VIDEO_DECOMPRESS, { "Video decompression", "vdecompress" }},
{ LIBRARY_CLASS_VIDEO_COMPRESS, { "Video compression", "vcompress" }},
{ LIBRARY_CLASS_VIDEO_POSTPROCESS, { "Video postprocess", "vo_pp" }},
{ LIBRARY_CLASS_VIDEO_RXTX, { "Video RXTX", "video_rxtx" }},
}
@ LIBRARY_CLASS_VIDEO_POSTPROCESS
Definition lib_common.h:83
@ LIBRARY_CLASS_VIDEO_DECOMPRESS
Definition lib_common.h:81
@ LIBRARY_CLASS_VIDEO_CAPTURE
Definition lib_common.h:78
@ LIBRARY_CLASS_AUDIO_PLAYBACK
Definition lib_common.h:77
@ LIBRARY_CLASS_VIDEO_RXTX
Definition lib_common.h:84
@ LIBRARY_CLASS_AUDIO_CAPTURE
Definition lib_common.h:76
@ LIBRARY_CLASS_VIDEO_DISPLAY
Definition lib_common.h:79
@ LIBRARY_CLASS_UNDEFINED
Definition lib_common.h:74
@ LIBRARY_CLASS_CAPTURE_FILTER
Definition lib_common.h:75
@ LIBRARY_CLASS_AUDIO_COMPRESS
Definition lib_common.h:80
@ LIBRARY_CLASS_VIDEO_COMPRESS
Definition lib_common.h:82

Definition at line 67 of file lib_common.cpp.

67 {
68 { LIBRARY_CLASS_UNDEFINED, { "General module", "" }},
69 { LIBRARY_CLASS_CAPTURE_FILTER, { "Video capture filter" , "vcapfilter" }},
70 { LIBRARY_CLASS_AUDIO_CAPTURE, { "Audio capture device", "acap" }},
71 { LIBRARY_CLASS_AUDIO_PLAYBACK, { "Audio playback device", "aplay" }},
72 { LIBRARY_CLASS_VIDEO_CAPTURE, { "Video capture device", "vidcap" }},
73 { LIBRARY_CLASS_VIDEO_DISPLAY, { "Video display device", "display" }},
74 { LIBRARY_CLASS_AUDIO_COMPRESS, { "Audio compression", "acompress" }},
75 { LIBRARY_CLASS_VIDEO_DECOMPRESS, { "Video decompression", "vdecompress" }},
76 { LIBRARY_CLASS_VIDEO_COMPRESS, { "Video compression", "vcompress" }},
77 { LIBRARY_CLASS_VIDEO_POSTPROCESS, { "Video postprocess", "vo_pp" }},
78 { LIBRARY_CLASS_VIDEO_RXTX, { "Video RXTX", "video_rxtx" }},
79};

Referenced by list_all_modules(), and load_library().