From ff0a46a05367746723612aabdbbe22cd546e4b10 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 1 Feb 2018 14:06:30 -0500 Subject: [PATCH] TT#30901 convert codec list to hash table Change-Id: I0956b441101b4c33e0f186399ef5558f89744e12 --- daemon/.gitignore | 1 + daemon/Makefile | 4 ++-- daemon/aux.h | 7 +------ lib/auxlib.h | 7 +++++++ lib/codeclib.c | 23 ++++++++++++++++------- lib/codeclib.h | 1 + {daemon => lib}/str.c | 2 +- recording-daemon/.gitignore | 1 + recording-daemon/Makefile | 4 +++- 9 files changed, 33 insertions(+), 17 deletions(-) rename {daemon => lib}/str.c (98%) diff --git a/daemon/.gitignore b/daemon/.gitignore index 91ee68b88..6226ece1d 100644 --- a/daemon/.gitignore +++ b/daemon/.gitignore @@ -9,3 +9,4 @@ loglib.c rtplib.c codeclib.c resample.c +str.c diff --git a/daemon/Makefile b/daemon/Makefile index 199820732..93e97ba28 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -63,11 +63,11 @@ LDFLAGS+= `pkg-config --libs libavfilter` include ../lib/lib.Makefile SRCS= main.c kernel.c poller.c aux.c control_tcp.c streambuf.c call.c control_udp.c redis.c \ - bencode.c cookie_cache.c udp_listener.c control_ng.c sdp.c str.c stun.c rtcp.c \ + bencode.c cookie_cache.c udp_listener.c control_ng.c sdp.c stun.c rtcp.c \ crypto.c rtp.c call_interfaces.c dtls.c log.c cli.c graphite.c ice.c socket.c \ media_socket.c homer.c recording.c statistics.c cdr.c ssrc.c iptables.c tcp_listener.c \ codec.c -LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c +LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c OBJS= $(SRCS:.c=.o) $(LIBSRCS:.c=.o) diff --git a/daemon/aux.h b/daemon/aux.h index ea82a4b9b..5a2aace6c 100644 --- a/daemon/aux.h +++ b/daemon/aux.h @@ -18,8 +18,7 @@ #include #include #include "compat.h" -#include -#include +#include "auxlib.h" #if !(GLIB_CHECK_VERSION(2,30,0)) #define g_atomic_int_and(atomic, val) \ @@ -195,10 +194,6 @@ INLINE int strmemcmp(const void *mem, int len, const char *str) { return memcmp(mem, str, len); } -INLINE void random_string(unsigned char *buf, int len) { - int ret = RAND_bytes(buf, len); - assert(ret == 1); -} INLINE long unsigned int ssl_random() { long unsigned int ret; random_string((void *) &ret, sizeof(ret)); diff --git a/lib/auxlib.h b/lib/auxlib.h index eae9ef72f..12dec6efc 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -2,6 +2,9 @@ #define _AUXLIB_H_ #include +#include +#include "compat.h" +#include struct rtpengine_common_config { char *config_file; @@ -21,5 +24,9 @@ void config_load(int *argc, char ***argv, GOptionEntry *entries, const char *des char *default_config, char *default_section, struct rtpengine_common_config *); +INLINE void random_string(unsigned char *buf, int len) { + int ret = RAND_bytes(buf, len); + assert(ret == 1); +} #endif diff --git a/lib/codeclib.c b/lib/codeclib.c index d717ea69f..0c24607f1 100644 --- a/lib/codeclib.c +++ b/lib/codeclib.c @@ -31,7 +31,7 @@ packetizer_f packetizer_samplestream; // flat stream of samples -static const struct codec_def_s codecs[] = { +static codec_def_t __codec_defs[] = { { .rtpname = "PCMA", .avcodec_id = AV_CODEC_ID_PCM_ALAW, @@ -290,13 +290,13 @@ static const struct codec_def_s codecs[] = { -// XXX use hashtable for quicker lookup +static GHashTable *codecs_ht; + + + const codec_def_t *codec_find(const str *name) { - for (int i = 0; i < G_N_ELEMENTS(codecs); i++) { - if (!str_cmp(name, codecs[i].rtpname)) - return &codecs[i]; - } - return NULL; + codec_def_t *ret = g_hash_table_lookup(codecs_ht, name); + return ret; } @@ -548,6 +548,15 @@ void codeclib_init() { avfilter_register_all(); avformat_network_init(); av_log_set_callback(avlog_ilog); + + codecs_ht = g_hash_table_new(str_hash, str_equal); + + for (int i = 0; i < G_N_ELEMENTS(__codec_defs); i++) { + codec_def_t *def = &__codec_defs[i]; + str_init(&def->rtpname_str, (char *) def->rtpname); + assert(g_hash_table_lookup(codecs_ht, &def->rtpname_str) == NULL); + g_hash_table_insert(codecs_ht, &def->rtpname_str, def); + } } diff --git a/lib/codeclib.h b/lib/codeclib.h index eebe40c79..885d92198 100644 --- a/lib/codeclib.h +++ b/lib/codeclib.h @@ -40,6 +40,7 @@ struct codec_def_s { const int default_ptime; packetizer_f * const packetizer; const int bits_per_sample; + str rtpname_str; }; struct format_s { diff --git a/daemon/str.c b/lib/str.c similarity index 98% rename from daemon/str.c rename to lib/str.c index 631e039c1..6de12c085 100644 --- a/daemon/str.c +++ b/lib/str.c @@ -1,5 +1,5 @@ #include "str.h" -#include "aux.h" +#include "auxlib.h" #include #include diff --git a/recording-daemon/.gitignore b/recording-daemon/.gitignore index dccf6fc2b..0c89ebf9a 100644 --- a/recording-daemon/.gitignore +++ b/recording-daemon/.gitignore @@ -9,3 +9,4 @@ loglib.c rtplib.c codeclib.c resample.c +str.c diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile index 15f25061d..a68aed0e8 100644 --- a/recording-daemon/Makefile +++ b/recording-daemon/Makefile @@ -11,6 +11,7 @@ CFLAGS+= `pkg-config --cflags libavutil` CFLAGS+= `pkg-config --cflags libavresample` CFLAGS+= `pkg-config --cflags libavfilter` CFLAGS+= `mysql_config --cflags` +CFLAGS+= `pkg-config --cflags openssl` LDFLAGS= -lm LDFLAGS+= `pkg-config --libs glib-2.0` @@ -21,12 +22,13 @@ LDFLAGS+= `pkg-config --libs libavutil` LDFLAGS+= `pkg-config --libs libavresample` LDFLAGS+= `pkg-config --libs libavfilter` LDFLAGS+= `mysql_config --libs` +LDFLAGS+= `pkg-config --libs openssl` include ../lib/lib.Makefile SRCS= epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c \ decoder.c output.c mix.c db.c log.c forward.c -LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c +LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c OBJS= $(SRCS:.c=.o) $(LIBSRCS:.c=.o)