OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h" | 11 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h" |
12 | 12 |
13 #include <assert.h> | 13 #include "webrtc/base/checks.h" |
14 #include <stdlib.h> | 14 #include "webrtc/system_wrappers/include/clock.h" |
15 | |
16 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 15 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
17 | 16 |
18 #ifdef _WIN32 | |
19 #include <windows.h> | |
20 #include <MMSystem.h> // timeGetTime | |
21 | |
22 // TODO(hellner): investigate if it is necessary to disable these warnings. | |
23 #pragma warning(disable : 4311) | |
24 #pragma warning(disable : 4312) | |
25 #else | |
26 #include <stdio.h> | |
27 #include <string.h> | |
28 #include <time.h> | |
29 #include <sys/time.h> | |
30 #endif | |
31 | |
32 namespace webrtc { | 17 namespace webrtc { |
33 SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) { | 18 namespace { |
34 SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation); | 19 uint64_t Seed() { |
35 return impl; | 20 return Clock::GetRealTimeClock()->TimeInMicroseconds(); |
36 } | 21 } |
| 22 } // namespace |
37 | 23 |
38 SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { | 24 SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { |
39 return StaticInstance(kAddRef); | 25 return GetStaticInstance<SSRCDatabase>(kAddRef); |
40 } | 26 } |
41 | 27 |
42 void SSRCDatabase::ReturnSSRCDatabase() { | 28 void SSRCDatabase::ReturnSSRCDatabase() { |
43 StaticInstance(kRelease); | 29 GetStaticInstance<SSRCDatabase>(kRelease); |
44 } | 30 } |
45 | 31 |
46 uint32_t SSRCDatabase::CreateSSRC() { | 32 uint32_t SSRCDatabase::CreateSSRC() { |
47 CriticalSectionScoped lock(_critSect); | 33 CriticalSectionScoped lock(crit_.get()); |
48 | 34 |
49 uint32_t ssrc = GenerateRandom(); | 35 while (true) { // Try until get a new ssrc. |
50 | 36 // 0 and 0xffffffff are invalid values for SSRC. |
51 while (_ssrcMap.find(ssrc) != _ssrcMap.end()) { | 37 uint32_t ssrc = random_.Rand(1u, 0xfffffffe); |
52 ssrc = GenerateRandom(); | 38 if (ssrcs_.insert(ssrc).second) { |
| 39 return ssrc; |
| 40 } |
53 } | 41 } |
54 _ssrcMap[ssrc] = 0; | |
55 | |
56 return ssrc; | |
57 } | 42 } |
58 | 43 |
59 int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) { | 44 void SSRCDatabase::RegisterSSRC(uint32_t ssrc) { |
60 CriticalSectionScoped lock(_critSect); | 45 CriticalSectionScoped lock(crit_.get()); |
61 _ssrcMap[ssrc] = 0; | 46 ssrcs_.insert(ssrc); |
62 return 0; | |
63 } | 47 } |
64 | 48 |
65 int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) { | 49 void SSRCDatabase::ReturnSSRC(uint32_t ssrc) { |
66 CriticalSectionScoped lock(_critSect); | 50 CriticalSectionScoped lock(crit_.get()); |
67 _ssrcMap.erase(ssrc); | 51 ssrcs_.erase(ssrc); |
68 return 0; | |
69 } | 52 } |
70 | 53 |
71 SSRCDatabase::SSRCDatabase() { | 54 SSRCDatabase::SSRCDatabase() |
72 // we need to seed the random generator, otherwise we get 26500 each time, | 55 : crit_(CriticalSectionWrapper::CreateCriticalSection()), random_(Seed()) {} |
73 // hardly a random value :) | |
74 #ifdef _WIN32 | |
75 srand(timeGetTime()); | |
76 #else | |
77 struct timeval tv; | |
78 struct timezone tz; | |
79 gettimeofday(&tv, &tz); | |
80 srand(tv.tv_usec); | |
81 #endif | |
82 | 56 |
83 _critSect = CriticalSectionWrapper::CreateCriticalSection(); | 57 SSRCDatabase::~SSRCDatabase() { |
84 } | 58 } |
85 | 59 |
86 SSRCDatabase::~SSRCDatabase() { | |
87 _ssrcMap.clear(); | |
88 delete _critSect; | |
89 } | |
90 | |
91 uint32_t SSRCDatabase::GenerateRandom() { | |
92 uint32_t ssrc = 0; | |
93 do { | |
94 ssrc = rand(); | |
95 ssrc = ssrc << 16; | |
96 ssrc += rand(); | |
97 } while (ssrc == 0 || ssrc == 0xffffffff); | |
98 | |
99 return ssrc; | |
100 } | |
101 } // namespace webrtc | 60 } // namespace webrtc |
OLD | NEW |