[rtp_rtcp] SSRCDatabase class cleaned (including all lint errors)
BUG=webrtc:5277 R=mflodman Review URL: https://codereview.webrtc.org/1507313005 Cr-Commit-Position: refs/heads/master@{#11023}
This commit is contained in:
@ -10,92 +10,51 @@
|
||||
|
||||
#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/system_wrappers/include/clock.h"
|
||||
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <MMSystem.h> // timeGetTime
|
||||
|
||||
// TODO(hellner): investigate if it is necessary to disable these warnings.
|
||||
#pragma warning(disable : 4311)
|
||||
#pragma warning(disable : 4312)
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
namespace webrtc {
|
||||
SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) {
|
||||
SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation);
|
||||
return impl;
|
||||
namespace {
|
||||
uint64_t Seed() {
|
||||
return Clock::GetRealTimeClock()->TimeInMicroseconds();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
|
||||
return StaticInstance(kAddRef);
|
||||
return GetStaticInstance<SSRCDatabase>(kAddRef);
|
||||
}
|
||||
|
||||
void SSRCDatabase::ReturnSSRCDatabase() {
|
||||
StaticInstance(kRelease);
|
||||
GetStaticInstance<SSRCDatabase>(kRelease);
|
||||
}
|
||||
|
||||
uint32_t SSRCDatabase::CreateSSRC() {
|
||||
CriticalSectionScoped lock(_critSect);
|
||||
CriticalSectionScoped lock(crit_.get());
|
||||
|
||||
uint32_t ssrc = GenerateRandom();
|
||||
|
||||
while (_ssrcMap.find(ssrc) != _ssrcMap.end()) {
|
||||
ssrc = GenerateRandom();
|
||||
while (true) { // Try until get a new ssrc.
|
||||
// 0 and 0xffffffff are invalid values for SSRC.
|
||||
uint32_t ssrc = random_.Rand(1u, 0xfffffffe);
|
||||
if (ssrcs_.insert(ssrc).second) {
|
||||
return ssrc;
|
||||
}
|
||||
}
|
||||
_ssrcMap[ssrc] = 0;
|
||||
|
||||
return ssrc;
|
||||
}
|
||||
|
||||
int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) {
|
||||
CriticalSectionScoped lock(_critSect);
|
||||
_ssrcMap[ssrc] = 0;
|
||||
return 0;
|
||||
void SSRCDatabase::RegisterSSRC(uint32_t ssrc) {
|
||||
CriticalSectionScoped lock(crit_.get());
|
||||
ssrcs_.insert(ssrc);
|
||||
}
|
||||
|
||||
int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) {
|
||||
CriticalSectionScoped lock(_critSect);
|
||||
_ssrcMap.erase(ssrc);
|
||||
return 0;
|
||||
void SSRCDatabase::ReturnSSRC(uint32_t ssrc) {
|
||||
CriticalSectionScoped lock(crit_.get());
|
||||
ssrcs_.erase(ssrc);
|
||||
}
|
||||
|
||||
SSRCDatabase::SSRCDatabase() {
|
||||
// we need to seed the random generator, otherwise we get 26500 each time,
|
||||
// hardly a random value :)
|
||||
#ifdef _WIN32
|
||||
srand(timeGetTime());
|
||||
#else
|
||||
struct timeval tv;
|
||||
struct timezone tz;
|
||||
gettimeofday(&tv, &tz);
|
||||
srand(tv.tv_usec);
|
||||
#endif
|
||||
|
||||
_critSect = CriticalSectionWrapper::CreateCriticalSection();
|
||||
}
|
||||
SSRCDatabase::SSRCDatabase()
|
||||
: crit_(CriticalSectionWrapper::CreateCriticalSection()), random_(Seed()) {}
|
||||
|
||||
SSRCDatabase::~SSRCDatabase() {
|
||||
_ssrcMap.clear();
|
||||
delete _critSect;
|
||||
}
|
||||
|
||||
uint32_t SSRCDatabase::GenerateRandom() {
|
||||
uint32_t ssrc = 0;
|
||||
do {
|
||||
ssrc = rand();
|
||||
ssrc = ssrc << 16;
|
||||
ssrc += rand();
|
||||
} while (ssrc == 0 || ssrc == 0xffffffff);
|
||||
|
||||
return ssrc;
|
||||
}
|
||||
} // namespace webrtc
|
||||
|
||||
@ -11,8 +11,10 @@
|
||||
#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_SSRC_DATABASE_H_
|
||||
#define WEBRTC_MODULES_RTP_RTCP_SOURCE_SSRC_DATABASE_H_
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "webrtc/base/random.h"
|
||||
#include "webrtc/base/scoped_ptr.h"
|
||||
#include "webrtc/system_wrappers/include/static_instance.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
@ -25,8 +27,8 @@ class SSRCDatabase {
|
||||
static void ReturnSSRCDatabase();
|
||||
|
||||
uint32_t CreateSSRC();
|
||||
int32_t RegisterSSRC(const uint32_t ssrc);
|
||||
int32_t ReturnSSRC(const uint32_t ssrc);
|
||||
void RegisterSSRC(uint32_t ssrc);
|
||||
void ReturnSSRC(uint32_t ssrc);
|
||||
|
||||
protected:
|
||||
SSRCDatabase();
|
||||
@ -39,13 +41,10 @@ class SSRCDatabase {
|
||||
// template class.
|
||||
friend SSRCDatabase* GetStaticInstance<SSRCDatabase>(
|
||||
CountOperation count_operation);
|
||||
static SSRCDatabase* StaticInstance(CountOperation count_operation);
|
||||
|
||||
uint32_t GenerateRandom();
|
||||
|
||||
std::map<uint32_t, uint32_t> _ssrcMap;
|
||||
|
||||
CriticalSectionWrapper* _critSect;
|
||||
rtc::scoped_ptr<CriticalSectionWrapper> crit_;
|
||||
Random random_ GUARDED_BY(crit_);
|
||||
std::set<uint32_t> ssrcs_ GUARDED_BY(crit_);
|
||||
};
|
||||
} // namespace webrtc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user