UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
pdb.c File Reference
#include "config.h"
#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
#include "rtp/rtp.h"
#include "rtp/pbuf.h"
#include "tfrc.h"
#include "pdb.h"
Include dependency graph for pdb.c:

Go to the source code of this file.

Classes

struct  pdb
 
struct  s_pdb_node
 

Macros

#define BTREE_MAGIC   0x10101010
 
#define BTREE_NODE_MAGIC   0x01010101
 
#define PDB_MAGIC   0x10101010
 
#define PDB_NODE_MAGIC   0x01010101
 

Typedefs

typedef struct s_pdb_node pdb_node_t
 

Functions

int pdb_add (struct pdb *db, uint32_t ssrc)
 
static struct pdb_e * pdb_create_item (uint32_t ssrc, volatile int *delay_ms)
 
static pdb_node_t * pdb_delete_node (struct pdb *tree, pdb_node_t *z)
 
void pdb_destroy (struct pdb **db_p)
 
void pdb_destroy_item (struct pdb_e *item)
 
struct pdb_e * pdb_get (struct pdb *db, uint32_t ssrc)
 
struct pdb * pdb_init (volatile int *delay_ms)
 
static void pdb_insert_node (struct pdb *tree, pdb_node_t *z)
 
void pdb_iter_done (pdb_iter_t *it)
 
struct pdb_e * pdb_iter_init (struct pdb *db, pdb_iter_t *it)
 
struct pdb_e * pdb_iter_next (pdb_iter_t *it)
 
static pdb_node_t * pdb_min (pdb_node_t *x)
 
int pdb_remove (struct pdb *db, uint32_t ssrc, struct pdb_e **item)
 
static pdb_node_t * pdb_search (pdb_node_t *x, uint32_t key)
 
static pdb_node_t * pdb_successor (pdb_node_t *x)
 
static void pdb_validate (struct pdb *t)
 

Macro Definition Documentation

◆ BTREE_MAGIC

#define BTREE_MAGIC   0x10101010

Definition at line 95 of file pdb.c.

Referenced by pdb_validate().

◆ BTREE_NODE_MAGIC

#define BTREE_NODE_MAGIC   0x01010101

Definition at line 96 of file pdb.c.

Referenced by pdb_add().

◆ PDB_MAGIC

#define PDB_MAGIC   0x10101010

Definition at line 72 of file pdb.c.

Referenced by pdb_init().

◆ PDB_NODE_MAGIC

#define PDB_NODE_MAGIC   0x01010101

Definition at line 73 of file pdb.c.

Typedef Documentation

◆ pdb_node_t

typedef struct s_pdb_node pdb_node_t

Function Documentation

◆ pdb_add()

int pdb_add ( struct pdb * db,
uint32_t ssrc )

Definition at line 299 of file pdb.c.

300{
301 /* Add an item to the participant database, indexed by ssrc. */
302 /* Returns 0 on success, 1 if the participant is already in */
303 /* the database, 2 for other failures. */
304 pdb_node_t *x;
305 struct pdb_e *i;
306
307 pdb_validate(db);
308 x = pdb_search(db->root, ssrc);
309 if (x != NULL) {
310 debug_msg("Item already exists - ssrc %x\n", ssrc);
311 return 1;
312 }
313
314 i = pdb_create_item(ssrc, db->delay_ms);
315 if (i == NULL) {
316 debug_msg("Unable to create database entry - ssrc %x\n", ssrc);
317 return 2;
318 }
319
320 x = (pdb_node_t *) malloc(sizeof(pdb_node_t));
321 x->key = ssrc;
322 x->data = i;
323 x->parent = NULL;
324 x->left = NULL;
325 x->right = NULL;
327 pdb_insert_node(db, x);
328 debug_msg("Added participant %x\n", ssrc);
329 return 0;
330}
#define debug_msg(...)
#define debug_msg(...) log_msg(LOG_LEVEL_DEBUG, "[pid/%d +%d %s] ", getpid(), LINE,...
Definition debug.h:92
static struct pdb_e * pdb_create_item(uint32_t ssrc, volatile int *delay_ms)
Definition pdb.c:277
#define BTREE_NODE_MAGIC
Definition pdb.c:96
static void pdb_validate(struct pdb *t)
Definition pdb.c:119
static pdb_node_t * pdb_search(pdb_node_t *x, uint32_t key)
Definition pdb.c:163
static void pdb_insert_node(struct pdb *tree, pdb_node_t *z)
Definition pdb.c:175
Definition pdb.h:72
uint32_t ssrc
Definition pdb.h:73
volatile int * delay_ms
Definition pdb.c:88
pdb_node_t * root
Definition pdb.c:85
struct s_pdb_node * left
Definition pdb.c:79
uint32_t key
Definition pdb.c:76
struct s_pdb_node * parent
Definition pdb.c:78
struct s_pdb_node * right
Definition pdb.c:80
void * data
Definition pdb.c:77
uint32_t magic
Definition pdb.c:81

References BTREE_NODE_MAGIC, s_pdb_node::data, debug_msg, pdb::delay_ms, s_pdb_node::key, s_pdb_node::left, s_pdb_node::magic, s_pdb_node::parent, pdb_create_item(), pdb_insert_node(), pdb_search(), pdb_validate(), s_pdb_node::right, pdb::root, and pdb_e::ssrc.

Referenced by initialize_audio_network(), rtp_video_rxtx::initialize_network(), process_sdes(), rtp_recv_callback(), and vidcap_rtsp_init().

Here is the call graph for this function:

◆ pdb_create_item()

static struct pdb_e * pdb_create_item ( uint32_t ssrc,
volatile int * delay_ms )
static

Definition at line 277 of file pdb.c.

278{
279 struct pdb_e *p = malloc(sizeof(struct pdb_e));
280 if (p != NULL) {
282 p->ssrc = ssrc;
283 p->sdes_cname = NULL;
284 p->sdes_name = NULL;
285 p->sdes_email = NULL;
286 p->sdes_phone = NULL;
287 p->sdes_loc = NULL;
288 p->sdes_tool = NULL;
289 p->sdes_note = NULL;
290 p->decoder_state = NULL;
291 p->decoder_state_deleter = NULL;
292 p->pt = 255;
293 p->playout_buffer = pbuf_init(delay_ms);
295 }
296 return p;
297}
struct pbuf * pbuf_init(volatile int *delay_ms)
Definition pbuf.c:174
time_ns_t creation_time
Definition pdb.h:86
char * sdes_name
Definition pdb.h:75
char * sdes_loc
Definition pdb.h:78
decoder_state_deleter_t decoder_state_deleter
decoder state deleter
Definition pdb.h:82
char * sdes_note
Definition pdb.h:80
struct tfrc * tfrc_state
Definition pdb.h:85
void * decoder_state
state of decoder for participant
Definition pdb.h:81
char * sdes_tool
Definition pdb.h:79
char * sdes_email
Definition pdb.h:76
char * sdes_phone
Definition pdb.h:77
uint8_t pt
Definition pdb.h:83
char * sdes_cname
Definition pdb.h:74
struct pbuf * playout_buffer
Definition pdb.h:84
struct tfrc * tfrc_init(time_ns_t curr_time)
Definition tfrc.c:365
static time_ns_t get_time_in_ns()
Definition tv.h:101

References pdb_e::creation_time, pdb_e::decoder_state, pdb_e::decoder_state_deleter, get_time_in_ns(), pbuf_init(), pdb_e::playout_buffer, pdb_e::pt, pdb_e::sdes_cname, pdb_e::sdes_email, pdb_e::sdes_loc, pdb_e::sdes_name, pdb_e::sdes_note, pdb_e::sdes_phone, pdb_e::sdes_tool, pdb_e::ssrc, tfrc_init(), and pdb_e::tfrc_state.

Referenced by pdb_add().

Here is the call graph for this function:

◆ pdb_delete_node()

static pdb_node_t * pdb_delete_node ( struct pdb * tree,
pdb_node_t * z )
static

Definition at line 204 of file pdb.c.

205{
206 pdb_node_t *x, *y;
207
208 pdb_validate(tree);
209 if (z->left == NULL || z->right == NULL) {
210 y = z;
211 } else {
212 y = pdb_successor(z);
213 }
214
215 if (y->left != NULL) {
216 x = y->left;
217 } else {
218 x = y->right;
219 }
220
221 if (x != NULL) {
222 x->parent = y->parent;
223 }
224
225 if (y->parent == NULL) {
226 tree->root = x;
227 } else if (y == y->parent->left) {
228 y->parent->left = x;
229 } else {
230 y->parent->right = x;
231 }
232
233 z->key = y->key;
234 z->data = y->data;
235
236 tree->count--;
237
238 pdb_validate(tree);
239 return y;
240}
static pdb_node_t * pdb_successor(pdb_node_t *x)
Definition pdb.c:146
int count
Definition pdb.c:87

References pdb::count, s_pdb_node::data, s_pdb_node::key, s_pdb_node::left, s_pdb_node::parent, pdb_successor(), pdb_validate(), s_pdb_node::right, and pdb::root.

Referenced by pdb_remove().

Here is the call graph for this function:

◆ pdb_destroy()

void pdb_destroy ( struct pdb ** db_p)

Definition at line 256 of file pdb.c.

257{
258 struct pdb *db = *db_p;
259
260 pdb_validate(db);
261 if (db->root != NULL) {
262 pdb_iter_t it;
263 struct pdb_e *cp = pdb_iter_init(db, &it);
264 while (cp != NULL) {
265 struct pdb_e *item = NULL;
266 pdb_remove(db, cp->ssrc, &item);
267 cp = pdb_iter_next(&it);
269 }
270 pdb_iter_done(&it);
271 }
272
273 free(db);
274 *db_p = NULL;
275}
void pdb_iter_done(pdb_iter_t *it)
Definition pdb.c:412
struct pdb_e * pdb_iter_next(pdb_iter_t *it)
Definition pdb.c:402
struct pdb_e * pdb_iter_init(struct pdb *db, pdb_iter_t *it)
Definition pdb.c:393
void pdb_destroy_item(struct pdb_e *item)
Definition pdb.c:370
int pdb_remove(struct pdb *db, uint32_t ssrc, struct pdb_e **item)
Definition pdb.c:346
void * pdb_iter_t
Definition pdb.h:106
Definition hd-rum.c:27
Definition pdb.c:84

References pdb_destroy_item(), pdb_iter_done(), pdb_iter_init(), pdb_iter_next(), pdb_remove(), pdb_validate(), pdb::root, and pdb_e::ssrc.

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

Here is the call graph for this function:

◆ pdb_destroy_item()

void pdb_destroy_item ( struct pdb_e * item)

Definition at line 370 of file pdb.c.

371{
372 if (item) {
373 free(item->sdes_cname);
374 free(item->sdes_name);
375 free(item->sdes_email);
376 free(item->sdes_phone);
377 free(item->sdes_loc);
378 free(item->sdes_tool);
379 free(item->sdes_note);
380 if (item->decoder_state_deleter && item->decoder_state) {
381 item->decoder_state_deleter(item->decoder_state);
382 }
383 pbuf_destroy(item->playout_buffer);
384 tfrc_done(item->tfrc_state);
385 free(item);
386 }
387}
void pbuf_destroy(struct pbuf *playout_buf)
Definition pbuf.c:195
void tfrc_done(struct tfrc *state)
Definition tfrc.c:415

References pbuf_destroy(), and tfrc_done().

Referenced by pdb_destroy(), and rtp_recv_callback().

Here is the call graph for this function:

◆ pdb_get()

struct pdb_e * pdb_get ( struct pdb * db,
uint32_t ssrc )

Definition at line 332 of file pdb.c.

333{
334 /* Return a pointer to the item indexed by ssrc, or NULL if */
335 /* the item is not present in the database. */
336 pdb_node_t *x;
337
338 pdb_validate(db);
339 x = pdb_search(db->root, ssrc);
340 if (x != NULL) {
341 return x->data;
342 }
343 return NULL;
344}

References s_pdb_node::data, pdb_search(), pdb_validate(), pdb::root, and pdb_e::ssrc.

Referenced by process_sdes(), and rtp_recv_callback().

Here is the call graph for this function:

◆ pdb_init()

struct pdb * pdb_init ( volatile int * delay_ms)
Parameters
delay_msdelay to be added to playback. Main reason is to give user a possibility to sync audio and video.

Definition at line 244 of file pdb.c.

245{
246 struct pdb *db = malloc(sizeof(struct pdb));
247 if (db != NULL) {
248 db->magic = PDB_MAGIC;
249 db->count = 0;
250 db->root = NULL;
251 db->delay_ms = delay_ms;
252 }
253 return db;
254}
#define PDB_MAGIC
Definition pdb.c:72
uint32_t magic
Definition pdb.c:86

References pdb::count, pdb::delay_ms, pdb::magic, PDB_MAGIC, and pdb::root.

Referenced by rtp_video_rxtx::rtp_video_rxtx(), audio_init(), and vidcap_rtsp_init().

◆ pdb_insert_node()

static void pdb_insert_node ( struct pdb * tree,
pdb_node_t * z )
static

Definition at line 175 of file pdb.c.

176{
177 pdb_node_t *x, *y;
178
179 pdb_validate(tree);
180 y = NULL;
181 x = tree->root;
182 while (x != NULL) {
183 y = x;
184 assert(z->key != x->key);
185 if (z->key < x->key) {
186 x = x->left;
187 } else {
188 x = x->right;
189 }
190 }
191
192 z->parent = y;
193 if (y == NULL) {
194 tree->root = z;
195 } else if (z->key < y->key) {
196 y->left = z;
197 } else {
198 y->right = z;
199 }
200 tree->count++;
201 pdb_validate(tree);
202}

References pdb::count, s_pdb_node::key, s_pdb_node::left, s_pdb_node::parent, pdb_validate(), s_pdb_node::right, and pdb::root.

Referenced by pdb_add().

Here is the call graph for this function:

◆ pdb_iter_done()

◆ pdb_iter_init()

struct pdb_e * pdb_iter_init ( struct pdb * db,
pdb_iter_t * it )

Definition at line 393 of file pdb.c.

394{
395 if (db->root == NULL) {
396 return NULL; /* The database is empty */
397 }
398 *it = (pdb_node_t *) pdb_min(db->root);
399 return ((pdb_node_t *) *it)->data;
400}
static pdb_node_t * pdb_min(pdb_node_t *x)
Definition pdb.c:135

References s_pdb_node::data, pdb_min(), and pdb::root.

Referenced by audio_receiver_process_message(), audio_receiver_thread(), pdb_destroy(), ultragrid_rtp_video_rxtx::receiver_loop(), ultragrid_rtp_video_rxtx::receiver_process_messages(), ultragrid_rtp_video_rxtx::remove_display_from_decoders(), and vidcap_rtsp_thread().

Here is the call graph for this function:

◆ pdb_iter_next()

struct pdb_e * pdb_iter_next ( pdb_iter_t * it)

Definition at line 402 of file pdb.c.

403{
404 assert(*it != NULL);
405 *it = (pdb_node_t *)pdb_successor((pdb_node_t *)*it);
406 if (*it == NULL) {
407 return NULL;
408 }
409 return ((pdb_node_t *) *it)->data;
410}

References s_pdb_node::data, and pdb_successor().

Referenced by audio_receiver_process_message(), audio_receiver_thread(), pdb_destroy(), ultragrid_rtp_video_rxtx::receiver_loop(), ultragrid_rtp_video_rxtx::receiver_process_messages(), ultragrid_rtp_video_rxtx::remove_display_from_decoders(), and vidcap_rtsp_thread().

Here is the call graph for this function:

◆ pdb_min()

static pdb_node_t * pdb_min ( pdb_node_t * x)
static

Definition at line 135 of file pdb.c.

136{
137 if (x == NULL) {
138 return NULL;
139 }
140 while (x->left) {
141 x = x->left;
142 }
143 return x;
144}

References s_pdb_node::left.

Referenced by pdb_iter_init(), and pdb_successor().

◆ pdb_remove()

int pdb_remove ( struct pdb * db,
uint32_t ssrc,
struct pdb_e ** item )

Definition at line 346 of file pdb.c.

347{
348 /* Remove the item indexed by ssrc. Return zero on success. */
349 pdb_node_t *x;
350
351 pdb_validate(db);
352 x = pdb_search(db->root, ssrc);
353 if (x == NULL) {
354 debug_msg("Item not on tree - ssrc %ul\n", ssrc);
355 *item = NULL;
356 return 1;
357 }
358
359 /* Note value that gets freed is not necessarily the the same as node
360 * that gets removed from tree since there is an optimization to avoid
361 * pointer updates in tree which means sometimes we just copy key and
362 * data from one node to another.
363 */
364 *item = x->data;
365 x = pdb_delete_node(db, x);
366 free(x);
367 return 0;
368}
static pdb_node_t * pdb_delete_node(struct pdb *tree, pdb_node_t *z)
Definition pdb.c:204

References s_pdb_node::data, debug_msg, pdb_delete_node(), pdb_search(), pdb_validate(), pdb::root, and pdb_e::ssrc.

Referenced by pdb_destroy(), and rtp_recv_callback().

Here is the call graph for this function:

◆ pdb_search()

static pdb_node_t * pdb_search ( pdb_node_t * x,
uint32_t key )
static

Definition at line 163 of file pdb.c.

164{
165 while (x != NULL && key != x->key) {
166 if (key < x->key) {
167 x = x->left;
168 } else {
169 x = x->right;
170 }
171 }
172 return x;
173}
const char * key
Definition cineform.cpp:125

References key, s_pdb_node::key, s_pdb_node::left, and s_pdb_node::right.

Referenced by pdb_add(), pdb_get(), and pdb_remove().

◆ pdb_successor()

static pdb_node_t * pdb_successor ( pdb_node_t * x)
static

Definition at line 146 of file pdb.c.

147{
148 pdb_node_t *y;
149
150 if (x->right != NULL) {
151 return pdb_min(x->right);
152 }
153
154 y = x->parent;
155 while (y != NULL && x == y->right) {
156 x = y;
157 y = y->parent;
158 }
159
160 return y;
161}

References s_pdb_node::parent, pdb_min(), and s_pdb_node::right.

Referenced by pdb_delete_node(), and pdb_iter_next().

Here is the call graph for this function:

◆ pdb_validate()

static void pdb_validate ( struct pdb * t)
static

Definition at line 119 of file pdb.c.

120{
121 assert(t->magic == BTREE_MAGIC);
122#ifdef DEBUG
123 pdb_count = 0;
124 if (t->root != NULL) {
125 pdb_validate_node(t->root, NULL);
126 }
127 assert(pdb_count == t->count);
128#endif
129}
#define BTREE_MAGIC
Definition pdb.c:95

References BTREE_MAGIC, pdb::count, pdb::magic, and pdb::root.

Referenced by pdb_add(), pdb_delete_node(), pdb_destroy(), pdb_get(), pdb_insert_node(), and pdb_remove().