From 4a82d793a332fd4e38da6eed77e3a4c6918276f6 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Tue, 27 Feb 2007 13:27:32 +0000 Subject: [PATCH] - added a centralized pseudo-random number function. - changed rand() calls to get_random(). - added/fixed ssrc (RTP header) generation. git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@252 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- core/AmRtpStream.cpp | 3 ++- core/AmRtpStream.h | 2 +- core/AmSession.cpp | 7 +------ core/AmSessionContainer.cpp | 9 --------- core/AmUtils.cpp | 27 +++++++++++++++++++++++++++ core/AmUtils.h | 5 +++++ core/sems.cpp | 2 ++ 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/core/AmRtpStream.cpp b/core/AmRtpStream.cpp index 67c859a5..bdb0582b 100644 --- a/core/AmRtpStream.cpp +++ b/core/AmRtpStream.cpp @@ -176,7 +176,7 @@ int AmRtpStream::send( unsigned int ts, unsigned char* buffer, unsigned int size rp.marker = false; rp.sequence = sequence++; rp.timestamp = ts; - rp.ssrc = 0; //l_ssrc; + rp.ssrc = l_ssrc; rp.compile((unsigned char*)buffer,size); rp.setAddr(&r_saddr); @@ -386,6 +386,7 @@ AmRtpStream::AmRtpStream(AmSession* _s) l_saddr.sin_addr.s_addr = INADDR_ANY; #endif + l_ssrc = get_random(); } AmRtpStream::~AmRtpStream() diff --git a/core/AmRtpStream.h b/core/AmRtpStream.h index 367a75b2..de2f1d9a 100644 --- a/core/AmRtpStream.h +++ b/core/AmRtpStream.h @@ -123,7 +123,7 @@ protected: /** the recv_offset initialized ? */ bool recv_offset_i; - // unsigned int l_ssrc; + unsigned int l_ssrc; unsigned int r_ssrc; bool r_ssrc_i; diff --git a/core/AmSession.cpp b/core/AmSession.cpp index ea5c4b6c..31dafe97 100644 --- a/core/AmSession.cpp +++ b/core/AmSession.cpp @@ -349,17 +349,12 @@ void AmSession::destroy() string AmSession::getNewId() { - static AmMutex _m; - struct timeval t; gettimeofday(&t,NULL); string id = ""; - _m.lock(); - id += int2hex(rand()) + "-"; - _m.unlock(); - + id += int2hex(get_random()) + "-"; id += int2hex(t.tv_sec) + int2hex(t.tv_usec) + "-"; id += int2hex((unsigned int) pthread_self()); diff --git a/core/AmSessionContainer.cpp b/core/AmSessionContainer.cpp index e3233a98..4246e158 100644 --- a/core/AmSessionContainer.cpp +++ b/core/AmSessionContainer.cpp @@ -59,15 +59,6 @@ void AmSessionContainer::on_stop() void AmSessionContainer::run() { - int seed=0; - FILE* fp_rand = fopen("/dev/random","r"); - if(fp_rand){ - fread(&seed,sizeof(int),1,fp_rand); - fclose(fp_rand); - } - seed += getpid(); - seed += time(0); - srand(seed); while(1){ diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index ae1aef24..cb279e0b 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -701,3 +701,30 @@ string get_header_param(const string& hdr_string, } return ""; } + + +// support for thread-safe pseudo-random numbers +static unsigned int _s_rand=0; +static AmMutex _s_rand_mut; + +void init_random() +{ + int seed=0; + FILE* fp_rand = fopen("/dev/random","r"); + if(fp_rand){ + fread(&seed,sizeof(int),1,fp_rand); + fclose(fp_rand); + } + seed += getpid(); + seed += time(0); + _s_rand = seed; +} + +unsigned int get_random() +{ + _s_rand_mut.lock(); + unsigned int r = rand_r(&_s_rand); + _s_rand_mut.unlock(); + + return r; +} diff --git a/core/AmUtils.h b/core/AmUtils.h index a61a64b2..2bc21e1a 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -250,6 +250,11 @@ string strip_header_params(const string& hdr_string); // get a header parameter value string get_header_param(const string& hdr_string, const string& param_name); +// support for thread-safe pseudo-random numbers +void init_random(); +unsigned int get_random(); + + #endif // Local Variables: diff --git a/core/sems.cpp b/core/sems.cpp index 63828708..f87361d4 100644 --- a/core/sems.cpp +++ b/core/sems.cpp @@ -371,6 +371,8 @@ int main(int argc, char* argv[]) if(AmPlugIn::instance()->load(AmConfig::PlugInPath, AmConfig::LoadPlugins)) return -1; + init_random(); + DBG("Starting session container\n"); AmSessionContainer::instance()->start();