UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
Module infrastructure

Files

file  module.h
 

Classes

struct  module
 
class  module_raii
 

Macros

#define CAST_MODULE(x)
 
#define MODULE_MAGIC   to_fourcc('M', 'O', 'D', ' ')
 

Typedefs

typedef void(* module_deleter_t) (struct module *)
 
typedef void(* notify_t) (struct module *)
 

Enumerations

enum  module_class {
  MODULE_CLASS_NONE = 0 , MODULE_CLASS_ROOT , MODULE_CLASS_PORT , MODULE_CLASS_COMPRESS ,
  MODULE_CLASS_DATA , MODULE_CLASS_SENDER , MODULE_CLASS_RECEIVER , MODULE_CLASS_TX ,
  MODULE_CLASS_AUDIO , MODULE_CLASS_CONTROL , MODULE_CLASS_CAPTURE , MODULE_CLASS_FILTER ,
  MODULE_CLASS_DISPLAY , MODULE_CLASS_DECODER , MODULE_CLASS_EXPORTER , MODULE_CLASS_KEYCONTROL
}
 

Functions

void append_message_path (char *buf, int buflen, const enum module_class *modules)
 
void dump_parents (struct module *node)
 
void dump_tree (struct module *root_node, int indent)
 
struct module * get_matching_child (struct module *node, const char *path)
 
struct module * get_module (struct module *root, const char *path)
 
struct module * get_parent_module (struct module *node)
 
struct module * get_root_module (struct module *node)
 
const char * module_class_name (enum module_class cls)
 
void module_done (struct module *module_data)
 
bool module_get_path_str (struct module *mod, char *buf, size_t buflen)
 
void module_init_default (struct module *module_data)
 
void module_register (struct module *module_data, struct module *parent)
 

Detailed Description

Module registration

Example:

struct state {
struct module mod;
...
} s;
s->mod.cls = MODULE_CLASS_<NAME>; // allways needed
s->mod.priv_data = s; // optional
s->mod.deleter = deleter; // only used for generic modules, see a note bellow
module_register(&s->mod, s->parent);
void ** state
int void(** deleter)(void *))
void module_init_default(struct module *module_data)
Definition module.c:48
void module_register(struct module *module_data, struct module *parent)
Definition module.c:82
Note
Struct module should be first inside the struct in order to be able to call CAST_MODULE to it (however, it is currently strictly required only for tx and video_compress_info modules).

Macro Definition Documentation

◆ CAST_MODULE

#define CAST_MODULE ( x)
Value:
((struct module *) x)

Definition at line 197 of file module.h.

Referenced by am_participant::~am_participant(), rtp_video_rxtx::~rtp_video_rxtx(), audio_done(), and audio_init().

◆ MODULE_MAGIC

#define MODULE_MAGIC   to_fourcc('M', 'O', 'D', ' ')

Definition at line 79 of file module.h.

Referenced by module_done(), and module_init_default().

Typedef Documentation

◆ module_deleter_t

typedef void(* module_deleter_t) (struct module *)

Definition at line 103 of file module.h.

◆ notify_t

typedef void(* notify_t) (struct module *)

Definition at line 104 of file module.h.

Enumeration Type Documentation

◆ module_class

Enumerator
MODULE_CLASS_NONE 
MODULE_CLASS_ROOT 
MODULE_CLASS_PORT 
MODULE_CLASS_COMPRESS 
MODULE_CLASS_DATA 
MODULE_CLASS_SENDER 
MODULE_CLASS_RECEIVER 
MODULE_CLASS_TX 
MODULE_CLASS_AUDIO 
MODULE_CLASS_CONTROL 
MODULE_CLASS_CAPTURE 
MODULE_CLASS_FILTER 
MODULE_CLASS_DISPLAY 
MODULE_CLASS_DECODER 
MODULE_CLASS_EXPORTER 
MODULE_CLASS_KEYCONTROL 

Definition at line 81 of file module.h.

81 {
98};
@ MODULE_CLASS_CONTROL
Definition module.h:91
@ MODULE_CLASS_NONE
Definition module.h:82
@ MODULE_CLASS_DATA
Definition module.h:86
@ MODULE_CLASS_ROOT
Definition module.h:83
@ MODULE_CLASS_DISPLAY
Definition module.h:94
@ MODULE_CLASS_FILTER
Definition module.h:93
@ MODULE_CLASS_EXPORTER
Definition module.h:96
@ MODULE_CLASS_COMPRESS
Definition module.h:85
@ MODULE_CLASS_KEYCONTROL
Definition module.h:97
@ MODULE_CLASS_RECEIVER
Definition module.h:88
@ MODULE_CLASS_AUDIO
Definition module.h:90
@ MODULE_CLASS_CAPTURE
Definition module.h:92
@ MODULE_CLASS_DECODER
Definition module.h:95
@ MODULE_CLASS_TX
Definition module.h:89
@ MODULE_CLASS_SENDER
Definition module.h:87
@ MODULE_CLASS_PORT
Definition module.h:84

Function Documentation

◆ append_message_path()

void append_message_path ( char * buf,
int buflen,
const enum module_class * modules )

Definition at line 187 of file module.c.

188{
189 const enum module_class *mod = modules;
190
191 while(*mod != MODULE_CLASS_NONE) {
192 if(strlen(buf) > 0) {
193 strncat(buf, ".", buflen - strlen(buf) - 1);
194 }
195 const char *node_name = module_class_name(*mod);
196 assert(node_name != NULL);
197
198 strncat(buf, node_name, buflen - strlen(buf) - 1);
199 mod += 1;
200 }
201}
module_class
Definition module.h:81
const char * module_class_name(enum module_class cls)
Definition module.c:178

References module_class_name(), and MODULE_CLASS_NONE.

Referenced by BasicRTSPOnlySubsession::deleteStream(), process_msg(), sdp_send_change_address_message(), and BasicRTSPOnlySubsession::startStream().

Here is the call graph for this function:

◆ dump_parents()

void dump_parents ( struct module * node)

Definition at line 348 of file module.c.

349{
350 bool first = true;
351 while (node != NULL) {
352 if (!first) {
353 printf("<-");
354 }
355 printf("%s", module_class_name(node->cls));
356 first = false;
357 node = get_parent_module(node);
358 }
359 printf("\n");
360}
struct module * get_parent_module(struct module *node)
Definition module.c:214
codec_t first
Definition sdl2.c:423
enum module_class cls
Definition module.h:115

References cls, first, get_parent_module(), and module_class_name().

Referenced by module_done().

Here is the call graph for this function:

◆ dump_tree()

void dump_tree ( struct module * root_node,
int indent )

Dumps modules' tree.

Important
no module in the tree should be locked (although currently recursive mutexes are used, so it will work if locked from same thread)

Definition at line 333 of file module.c.

333 {
334 for(int i = 0; i < indent; ++i)
335 putchar(' ');
336
337 printf("%s\n", module_class_name(root_node->cls));
338
339 module_mutex_lock(&root_node->lock);
340 for(void *it = simple_linked_list_it_init(root_node->children); it != NULL; ) {
341 struct module *child = simple_linked_list_it_next(&it);
342 dump_tree(child, indent + 2);
343 }
344 module_mutex_unlock(&root_node->lock);
345}
void dump_tree(struct module *root_node, int indent)
Definition module.c:333
void * simple_linked_list_it_next(void **i)
Definition list.cpp:119
void * simple_linked_list_it_init(struct simple_linked_list *l)
Definition list.cpp:108
static void module_mutex_lock(pthread_mutex_t *lock)
Definition module.c:69
static void module_mutex_unlock(pthread_mutex_t *lock)
Definition module.c:76
pthread_mutex_t lock
Definition module.h:114
struct simple_linked_list * children
Definition module.h:117

References children, cls, dump_tree(), lock, module_class_name(), module_mutex_lock(), module_mutex_unlock(), simple_linked_list_it_init(), and simple_linked_list_it_next().

Referenced by dump_tree(), module_done(), and process_msg().

Here is the call graph for this function:

◆ get_matching_child()

struct module * get_matching_child ( struct module * node,
const char * path )

IMPORTANT: module given as parameter should be locked within the calling thread.

Return values
NULLif not found
non-NULLpointer to the module

Definition at line 300 of file module.c.

301{
302 struct module *receiver = node;
303 char *path, *tmp;
304 char *item, *save_ptr = NULL;
305
306 assert(node != NULL);
307
308 tmp = path = strdup(const_path);
309 assert(path != NULL);
310 if ((item = strtok_r(path, ".", &save_ptr))) {
311 int id_num = 0;
312 char *id_name = NULL;
313 get_receiver_index(item, &id_num, &id_name);
314 receiver = find_child(receiver, item, id_num, id_name);
315 if (!receiver) {
316 free(tmp);
317 return NULL;
318 }
319 free(tmp);
320 return receiver;
321 }
322
323 free(tmp);
324
325 return NULL;
326}
static void get_receiver_index(char *node_str, int *index, char **name)
Definition module.c:251
static struct module * find_child(struct module *node, const char *node_name, int id_num, const char *id_name)
Definition module.c:223
Definition hd-rum.c:27

References find_child(), and get_receiver_index().

Referenced by get_module(), module_check_undelivered_messages(), and send_message_common().

Here is the call graph for this function:

◆ get_module()

struct module * get_module ( struct module * root,
const char * path )
Return values
NULLif not found
non-NULLpointer to the module

Definition at line 264 of file module.c.

265{
266 assert(root != NULL);
267 assert(const_path != NULL);
268
269 struct module *receiver = root;
270 char *path, *tmp;
271 char *item, *save_ptr;
272
273 module_mutex_lock(&root->lock);
274
275 tmp = path = strdup(const_path);
276 assert(path != NULL);
277 while ((item = strtok_r(path, ".", &save_ptr))) {
278 struct module *old_receiver = receiver;
279
280 receiver = get_matching_child(receiver, item);
281
282 if (!receiver) {
283 module_mutex_unlock(&old_receiver->lock);
284 free(tmp);
285 return NULL;
286 }
287 module_mutex_lock(&receiver->lock);
288 module_mutex_unlock(&old_receiver->lock);
289
290 path = NULL;
291
292 }
293 free(tmp);
294
295 module_mutex_unlock(&receiver->lock);
296
297 return receiver;
298}
struct module * get_matching_child(struct module *node, const char *const_path)
Definition module.c:300

References get_matching_child(), lock, module_mutex_lock(), and module_mutex_unlock().

Referenced by state_controlport_stats::state_controlport_stats(), state_video_decoder::state_video_decoder(), ultragrid_rtp_video_rxtx::ultragrid_rtp_video_rxtx(), audio_decoder_init(), display_sdl_init(), and tx_init().

Here is the call graph for this function:

◆ get_parent_module()

struct module * get_parent_module ( struct module * node)

Definition at line 214 of file module.c.

215{
216 return node->parent;
217}
struct module * parent
Definition module.h:116

References parent.

Referenced by dump_parents(), get_module_identifier(), set_fec(), and tx_update().

◆ get_root_module()

◆ module_class_name()

const char * module_class_name ( enum module_class cls)

Definition at line 178 of file module.c.

179{
180 if((unsigned int) cls < sizeof(module_class_name_pairs)/sizeof(const char *))
182 else
183 return NULL;
184}
enum library_class cls
Definition host.cpp:749
static const char * module_class_name_pairs[]
Definition module.c:160

References cls, and module_class_name_pairs.

Referenced by append_message_path(), dump_parents(), dump_tree(), find_child(), get_module_identifier(), and process_msg().

◆ module_done()

void module_done ( struct module * module_data)

Definition at line 93 of file module.c.

94{
95 if(!module_data)
96 return;
97
98 if (module_data->cls == MODULE_CLASS_NONE)
99 return;
100
101 assert(module_data->magic == MODULE_MAGIC);
102
103 if(module_data->parent) {
104 module_mutex_lock(&module_data->parent->lock);
105 bool found = simple_linked_list_remove(
106 module_data->parent->children, module_data);
107 assert(found);
108 module_mutex_unlock(&module_data->parent->lock);
109 }
110
111 // we assume that deleter may dealloc space where are structure stored
112 module_mutex_lock(&module_data->lock);
113 struct module tmp;
114 memcpy(&tmp, module_data, sizeof(struct module));
115 module_mutex_unlock(&module_data->lock);
116
117 module_data->cls = MODULE_CLASS_NONE;
118
119 if(module_data->deleter)
120 module_data->deleter(module_data);
121
122 if(simple_linked_list_size(tmp.children) > 0) {
123 log_msg(LOG_LEVEL_WARNING, "Warning: Child database not empty! Remaining:\n");
124 dump_tree(&tmp, 0);
125 module_mutex_lock(&tmp.lock);
126 for(void *it = simple_linked_list_it_init(module_data->children); it != NULL; ) {
127 struct module *child = simple_linked_list_it_next(&it);
128 module_mutex_lock(&child->lock);
129 child->parent = NULL;
130 module_mutex_unlock(&child->lock);
131 }
132 module_mutex_unlock(&tmp.lock);
133 }
134 simple_linked_list_destroy(tmp.children);
135
136 if(simple_linked_list_size(tmp.msg_queue) > 0) {
137 fprintf(stderr, "Warning: Message queue not empty!\n");
139 printf("Path: ");
140 dump_parents(&tmp);
141 }
142 struct message *m;
143 while ((m = check_message(&tmp))) {
144 free_message(m, NULL);
145 }
146 }
147 simple_linked_list_destroy(tmp.msg_queue);
148
149 while (simple_linked_list_size(tmp.msg_queue_children) > 0) {
150 struct message *m = simple_linked_list_pop(tmp.msg_queue_children);
151 free_message_for_child(m, NULL);
152 }
153 simple_linked_list_destroy(tmp.msg_queue_children);
154
155 pthread_mutex_destroy(&tmp.lock);
156
157 free(tmp.name);
158}
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
#define MODULE_MAGIC
Definition module.h:79
void dump_parents(struct module *node)
Definition module.c:348
volatile int log_level
Definition debug.cpp:65
bool simple_linked_list_remove(struct simple_linked_list *l, void *item)
Definition list.cpp:144
int simple_linked_list_size(struct simple_linked_list *l)
Definition list.cpp:93
void simple_linked_list_destroy(struct simple_linked_list *l)
Definition list.cpp:58
void * simple_linked_list_pop(struct simple_linked_list *l)
Definition list.cpp:82
void free_message(struct message *msg, struct response *r)
void free_message_for_child(void *m, struct response *r)
Definition messaging.cpp:99
struct message * check_message(struct module *mod)
uint32_t magic
Definition module.h:112
module_deleter_t deleter
Definition module.h:118

References check_message(), children, cls, deleter, dump_parents(), dump_tree(), free_message(), free_message_for_child(), lock, log_level, LOG_LEVEL_VERBOSE, LOG_LEVEL_WARNING, log_msg(), magic, MODULE_CLASS_NONE, MODULE_MAGIC, module_mutex_lock(), module_mutex_unlock(), msg_queue, msg_queue_children, name, parent, simple_linked_list_destroy(), simple_linked_list_it_init(), simple_linked_list_it_next(), simple_linked_list_pop(), simple_linked_list_remove(), and simple_linked_list_size().

Referenced by video_rxtx::video_rxtx(), am_participant::~am_participant(), hd_rum_translator_state::~hd_rum_translator_state(), module_raii::~module_raii(), replica::~replica(), rtp_video_rxtx::~rtp_video_rxtx(), state_gl::~state_gl(), state_sdl::~state_sdl(), state_uv::~state_uv(), state_video_decoder::~state_video_decoder(), video_rxtx::~video_rxtx(), audio_decoder_destroy(), audio_done(), audio_init(), capture_filter_destroy(), capture_filter_init(), cleanup_common(), compress_done(), control_done(), display_done(), display_sdl2_done(), done(), export_destroy(), initialize_video_capture(), initialize_video_display(), keyboard_control::impl::stop(), tx_init(), vidcap_done(), vidcap_file_common_cleanup(), and vidcap_switcher_done().

Here is the call graph for this function:

◆ module_get_path_str()

bool module_get_path_str ( struct module * mod,
char * buf,
size_t buflen )

Gets textual representation of path from root to module.

Currently only simple paths are created (class names only)

Definition at line 408 of file module.c.

408 {
409 assert(buflen > 0);
410 buf[0] = '\0';
411
412 while (mod) {
413 const char *cur_name = get_module_identifier(mod);
414 if (sizeof(buf) + 1 + sizeof(cur_name) >= buflen) {
415 return false;
416 }
417 // move content of buf strlen(cur_name) right
418 if (strlen(buf) > 0) {
419 memmove(buf + strlen(cur_name) + 1, buf, strlen(buf) + 1);
420 buf[strlen(cur_name)] = '.'; // and separate with '.'
421 } else { // rightmost (first written) element
422 buf[strlen(cur_name)] = '\0';
423 }
424 memcpy(buf, cur_name, strlen(cur_name));
425 mod = mod->parent;
426 }
427
428 return true;
429}
static const char * get_module_identifier(struct module *mod)
Definition module.c:366

References get_module_identifier(), and parent.

Referenced by keycontrol_register_key().

Here is the call graph for this function:

◆ module_init_default()

void module_init_default ( struct module * module_data)

Definition at line 48 of file module.c.

49{
50 int ret = 0;
51 memset(module_data, 0, sizeof(struct module));
52
53 pthread_mutexattr_t attr;
54 ret |= pthread_mutexattr_init(&attr);
55 ret |= pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
56 ret |= pthread_mutex_init(&module_data->lock, &attr);
57 ret |= pthread_mutex_init(&module_data->msg_queue_lock, &attr);
58 ret |= pthread_mutexattr_destroy(&attr);
59 assert(ret == 0 && "Unable to create mutex or set attributes");
60
61 module_data->children = simple_linked_list_init();
62 module_data->msg_queue = simple_linked_list_init();
64
65 module_data->magic = MODULE_MAGIC;
66}
struct simple_linked_list * simple_linked_list_init(void)
Definition list.cpp:53
struct simple_linked_list * msg_queue
Definition module.h:122
pthread_mutex_t msg_queue_lock
Definition module.h:121
struct simple_linked_list * msg_queue_children
messages for childern that were not delivered
Definition module.h:124

References children, lock, magic, MODULE_MAGIC, msg_queue, msg_queue_children, msg_queue_lock, and simple_linked_list_init().

Referenced by keyboard_control::impl::impl(), module_raii::module_raii(), replica::replica(), state_gl::state_gl(), state_sdl::state_sdl(), state_video_decoder::state_video_decoder(), video_rxtx::video_rxtx(), audio_decoder_init(), capture_filter_init(), cineform_compress_init(), compress_init(), control_init(), display_sdl2_init(), export_init(), init(), init_root_module(), initialize_import(), initialize_video_capture(), initialize_video_display(), j2k_compress_init(), tx_init(), vidcap_file_init(), and vidcap_switcher_init().

Here is the call graph for this function:

◆ module_register()

void module_register ( struct module * module_data,
struct module * parent )