2011-07-07 08:21:25 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-29 14:27:38 +00:00
|
|
|
#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-05-29 14:27:38 +00:00
|
|
|
#include <assert.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
#include <stdlib.h>
|
2013-05-29 14:27:38 +00:00
|
|
|
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2015-12-10 02:39:40 -08:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <MMSystem.h> // timeGetTime
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-12-09 17:46:20 +00:00
|
|
|
// TODO(hellner): investigate if it is necessary to disable these warnings.
|
2015-12-10 02:39:40 -08:00
|
|
|
#pragma warning(disable : 4311)
|
|
|
|
|
#pragma warning(disable : 4312)
|
2011-07-07 08:21:25 +00:00
|
|
|
#else
|
2015-12-10 02:39:40 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <sys/time.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
2015-12-10 02:39:40 -08:00
|
|
|
SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) {
|
|
|
|
|
SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation);
|
2011-12-09 17:46:20 +00:00
|
|
|
return impl;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
|
|
|
|
|
return StaticInstance(kAddRef);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
void SSRCDatabase::ReturnSSRCDatabase() {
|
|
|
|
|
StaticInstance(kRelease);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
uint32_t SSRCDatabase::CreateSSRC() {
|
|
|
|
|
CriticalSectionScoped lock(_critSect);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
uint32_t ssrc = GenerateRandom();
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
while (_ssrcMap.find(ssrc) != _ssrcMap.end()) {
|
|
|
|
|
ssrc = GenerateRandom();
|
|
|
|
|
}
|
|
|
|
|
_ssrcMap[ssrc] = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
return ssrc;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) {
|
|
|
|
|
CriticalSectionScoped lock(_critSect);
|
|
|
|
|
_ssrcMap[ssrc] = 0;
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) {
|
|
|
|
|
CriticalSectionScoped lock(_critSect);
|
|
|
|
|
_ssrcMap.erase(ssrc);
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
SSRCDatabase::SSRCDatabase() {
|
|
|
|
|
// we need to seed the random generator, otherwise we get 26500 each time,
|
|
|
|
|
// hardly a random value :)
|
2011-07-07 08:21:25 +00:00
|
|
|
#ifdef _WIN32
|
2015-12-10 02:39:40 -08:00
|
|
|
srand(timeGetTime());
|
2011-07-07 08:21:25 +00:00
|
|
|
#else
|
2015-12-10 02:39:40 -08:00
|
|
|
struct timeval tv;
|
|
|
|
|
struct timezone tz;
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
srand(tv.tv_usec);
|
2011-07-07 08:21:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
_critSect = CriticalSectionWrapper::CreateCriticalSection();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
SSRCDatabase::~SSRCDatabase() {
|
|
|
|
|
_ssrcMap.clear();
|
|
|
|
|
delete _critSect;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
uint32_t SSRCDatabase::GenerateRandom() {
|
|
|
|
|
uint32_t ssrc = 0;
|
|
|
|
|
do {
|
|
|
|
|
ssrc = rand();
|
|
|
|
|
ssrc = ssrc << 16;
|
|
|
|
|
ssrc += rand();
|
|
|
|
|
} while (ssrc == 0 || ssrc == 0xffffffff);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-12-10 02:39:40 -08:00
|
|
|
return ssrc;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-07-03 15:12:26 +00:00
|
|
|
} // namespace webrtc
|