1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
12 
13 #include "webrtc/base/checks.h"
14 #include "webrtc/system_wrappers/include/clock.h"
15 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
16 
17 namespace webrtc {
18 namespace {
Seed()19 uint64_t Seed() {
20   return Clock::GetRealTimeClock()->TimeInMicroseconds();
21 }
22 }  // namespace
23 
GetSSRCDatabase()24 SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
25   return GetStaticInstance<SSRCDatabase>(kAddRef);
26 }
27 
ReturnSSRCDatabase()28 void SSRCDatabase::ReturnSSRCDatabase() {
29   GetStaticInstance<SSRCDatabase>(kRelease);
30 }
31 
CreateSSRC()32 uint32_t SSRCDatabase::CreateSSRC() {
33   CriticalSectionScoped lock(crit_.get());
34 
35   while (true) {  // Try until get a new ssrc.
36     // 0 and 0xffffffff are invalid values for SSRC.
37     uint32_t ssrc = random_.Rand(1u, 0xfffffffe);
38     if (ssrcs_.insert(ssrc).second) {
39       return ssrc;
40     }
41   }
42 }
43 
RegisterSSRC(uint32_t ssrc)44 void SSRCDatabase::RegisterSSRC(uint32_t ssrc) {
45   CriticalSectionScoped lock(crit_.get());
46   ssrcs_.insert(ssrc);
47 }
48 
ReturnSSRC(uint32_t ssrc)49 void SSRCDatabase::ReturnSSRC(uint32_t ssrc) {
50   CriticalSectionScoped lock(crit_.get());
51   ssrcs_.erase(ssrc);
52 }
53 
SSRCDatabase()54 SSRCDatabase::SSRCDatabase()
55     : crit_(CriticalSectionWrapper::CreateCriticalSection()), random_(Seed()) {}
56 
~SSRCDatabase()57 SSRCDatabase::~SSRCDatabase() {
58 }
59 
60 }  // namespace webrtc
61