mirror of https://github.com/sipwise/rtpengine.git
Change-Id: I481dd4062f044f9f7add65b4b82d276629f47ce8changes/69/12269/15
parent
c8b36e0b96
commit
b3583633e3
@ -0,0 +1,74 @@
|
||||
#include "ssrc.h"
|
||||
#include <glib.h>
|
||||
#include "aux.h"
|
||||
|
||||
|
||||
|
||||
static struct ssrc_entry *create_ssrc_entry(u_int32_t ssrc) {
|
||||
struct ssrc_entry *ent;
|
||||
ent = g_slice_alloc0(sizeof(struct ssrc_entry));
|
||||
ent->ssrc = ssrc;
|
||||
return ent;
|
||||
}
|
||||
static void add_ssrc_entry(struct ssrc_entry *ent, struct ssrc_hash *ht) {
|
||||
g_hash_table_replace(ht->ht, &ent->ssrc, ent);
|
||||
}
|
||||
static void free_ssrc_entry(void *p) {
|
||||
g_slice_free1(sizeof(struct ssrc_entry), p);
|
||||
}
|
||||
|
||||
|
||||
struct ssrc_entry *find_ssrc(u_int32_t ssrc, struct ssrc_hash *ht) {
|
||||
rwlock_lock_r(&ht->lock);
|
||||
struct ssrc_entry *ret = g_hash_table_lookup(ht->ht, &ssrc);
|
||||
rwlock_unlock_r(&ht->lock);
|
||||
return ret;
|
||||
}
|
||||
struct ssrc_entry *get_ssrc(u_int32_t ssrc, struct ssrc_hash *ht /* , int *created */) {
|
||||
struct ssrc_entry *ent;
|
||||
|
||||
restart:
|
||||
ent = find_ssrc(ssrc, ht);
|
||||
if (G_LIKELY(ent)) {
|
||||
// if (created)
|
||||
// *created = 0;
|
||||
return ent;
|
||||
}
|
||||
|
||||
ent = create_ssrc_entry(ssrc);
|
||||
|
||||
rwlock_lock_w(&ht->lock);
|
||||
if (g_hash_table_lookup(ht->ht, &ssrc)) {
|
||||
// preempted
|
||||
rwlock_unlock_w(&ht->lock);
|
||||
free_ssrc_entry(ent);
|
||||
goto restart;
|
||||
}
|
||||
add_ssrc_entry(ent, ht);
|
||||
rwlock_unlock_w(&ht->lock);
|
||||
// if (created)
|
||||
// *created = 1;
|
||||
return ent;
|
||||
}
|
||||
void free_ssrc_hash(struct ssrc_hash **ht) {
|
||||
if (!*ht)
|
||||
return;
|
||||
g_hash_table_destroy((*ht)->ht);
|
||||
g_slice_free1(sizeof(**ht), *ht);
|
||||
*ht = NULL;
|
||||
}
|
||||
|
||||
|
||||
struct ssrc_hash *create_ssrc_hash(void) {
|
||||
struct ssrc_hash *ret;
|
||||
ret = g_slice_alloc0(sizeof(*ret));
|
||||
ret->ht = g_hash_table_new_full(uint32_hash, uint32_eq, NULL, free_ssrc_entry);
|
||||
rwlock_init(&ret->lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct ssrc_ctx *get_ssrc_ctx(u_int32_t ssrc, struct ssrc_hash *ht, enum ssrc_dir dir) {
|
||||
struct ssrc_entry *s = get_ssrc(ssrc, ht /* , NULL */);
|
||||
return ((void *) s) + dir;
|
||||
}
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
#ifndef _SSRC_H_
|
||||
#define _SSRC_H_
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <glib.h>
|
||||
#include "compat.h"
|
||||
#include "aux.h"
|
||||
|
||||
|
||||
|
||||
|
||||
struct ssrc_hash {
|
||||
GHashTable *ht;
|
||||
rwlock_t lock;
|
||||
};
|
||||
struct ssrc_ctx {
|
||||
// XXX lock this?
|
||||
u_int64_t srtp_index;
|
||||
// XXX move entire crypto context in here?
|
||||
};
|
||||
struct ssrc_entry {
|
||||
// XXX lock this?
|
||||
u_int32_t ssrc;
|
||||
struct ssrc_ctx input_ctx,
|
||||
output_ctx;
|
||||
};
|
||||
enum ssrc_dir {
|
||||
SSRC_DIR_INPUT = G_STRUCT_OFFSET(struct ssrc_entry, input_ctx),
|
||||
SSRC_DIR_OUTPUT = G_STRUCT_OFFSET(struct ssrc_entry, output_ctx),
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
void free_ssrc_hash(struct ssrc_hash **);
|
||||
struct ssrc_hash *create_ssrc_hash(void);
|
||||
|
||||
struct ssrc_entry *find_ssrc(u_int32_t, struct ssrc_hash *); // returns NULL if not found
|
||||
struct ssrc_entry *get_ssrc(u_int32_t, struct ssrc_hash * /* , int *created */); // creates new entry if not found
|
||||
//void add_ssrc_entry(struct ssrc_entry *, struct ssrc_hash *); // XXX static
|
||||
//struct ssrc_entry *create_ssrc_entry(u_int32_t);
|
||||
struct ssrc_ctx *get_ssrc_ctx(u_int32_t, struct ssrc_hash *, enum ssrc_dir); // creates new entry if not found
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Reference in new issue