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 <assert.h> |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 | 15 |
16 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 16 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
17 | 17 |
18 #ifdef _WIN32 | 18 #ifdef _WIN32 |
19 #include <windows.h> | 19 #include <windows.h> |
20 #include <MMSystem.h> //timeGetTime | 20 #include <MMSystem.h> // timeGetTime |
21 | 21 |
22 // TODO(hellner): investigate if it is necessary to disable these warnings. | 22 // TODO(hellner): investigate if it is necessary to disable these warnings. |
23 #pragma warning(disable:4311) | 23 #pragma warning(disable : 4311) |
24 #pragma warning(disable:4312) | 24 #pragma warning(disable : 4312) |
25 #else | 25 #else |
26 #include <stdio.h> | 26 #include <stdio.h> |
27 #include <string.h> | 27 #include <string.h> |
28 #include <time.h> | 28 #include <time.h> |
29 #include <sys/time.h> | 29 #include <sys/time.h> |
30 #endif | 30 #endif |
31 | 31 |
32 namespace webrtc { | 32 namespace webrtc { |
33 SSRCDatabase* | 33 SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) { |
34 SSRCDatabase::StaticInstance(CountOperation count_operation) | 34 SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation); |
35 { | |
36 SSRCDatabase* impl = | |
37 GetStaticInstance<SSRCDatabase>(count_operation); | |
38 return impl; | 35 return impl; |
39 } | 36 } |
40 | 37 |
41 SSRCDatabase* | 38 SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { |
42 SSRCDatabase::GetSSRCDatabase() | 39 return StaticInstance(kAddRef); |
43 { | |
44 return StaticInstance(kAddRef); | |
45 } | 40 } |
46 | 41 |
47 void | 42 void SSRCDatabase::ReturnSSRCDatabase() { |
48 SSRCDatabase::ReturnSSRCDatabase() | 43 StaticInstance(kRelease); |
49 { | |
50 StaticInstance(kRelease); | |
51 } | 44 } |
52 | 45 |
53 uint32_t | 46 uint32_t SSRCDatabase::CreateSSRC() { |
54 SSRCDatabase::CreateSSRC() | 47 CriticalSectionScoped lock(_critSect); |
55 { | |
56 CriticalSectionScoped lock(_critSect); | |
57 | 48 |
58 uint32_t ssrc = GenerateRandom(); | 49 uint32_t ssrc = GenerateRandom(); |
59 | 50 |
60 while(_ssrcMap.find(ssrc) != _ssrcMap.end()) | 51 while (_ssrcMap.find(ssrc) != _ssrcMap.end()) { |
61 { | 52 ssrc = GenerateRandom(); |
62 ssrc = GenerateRandom(); | 53 } |
63 } | 54 _ssrcMap[ssrc] = 0; |
64 _ssrcMap[ssrc] = 0; | |
65 | 55 |
66 return ssrc; | 56 return ssrc; |
67 } | 57 } |
68 | 58 |
69 int32_t | 59 int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) { |
70 SSRCDatabase::RegisterSSRC(const uint32_t ssrc) | 60 CriticalSectionScoped lock(_critSect); |
71 { | 61 _ssrcMap[ssrc] = 0; |
72 CriticalSectionScoped lock(_critSect); | 62 return 0; |
73 _ssrcMap[ssrc] = 0; | |
74 return 0; | |
75 } | 63 } |
76 | 64 |
77 int32_t | 65 int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) { |
78 SSRCDatabase::ReturnSSRC(const uint32_t ssrc) | 66 CriticalSectionScoped lock(_critSect); |
79 { | 67 _ssrcMap.erase(ssrc); |
80 CriticalSectionScoped lock(_critSect); | 68 return 0; |
81 _ssrcMap.erase(ssrc); | |
82 return 0; | |
83 } | 69 } |
84 | 70 |
85 SSRCDatabase::SSRCDatabase() | 71 SSRCDatabase::SSRCDatabase() { |
86 { | 72 // we need to seed the random generator, otherwise we get 26500 each time, |
87 // we need to seed the random generator, otherwise we get 26500 each time, h
ardly a random value :) | 73 // hardly a random value :) |
88 #ifdef _WIN32 | 74 #ifdef _WIN32 |
89 srand(timeGetTime()); | 75 srand(timeGetTime()); |
90 #else | 76 #else |
91 struct timeval tv; | 77 struct timeval tv; |
92 struct timezone tz; | 78 struct timezone tz; |
93 gettimeofday(&tv, &tz); | 79 gettimeofday(&tv, &tz); |
94 srand(tv.tv_usec); | 80 srand(tv.tv_usec); |
95 #endif | 81 #endif |
96 | 82 |
97 _critSect = CriticalSectionWrapper::CreateCriticalSection(); | 83 _critSect = CriticalSectionWrapper::CreateCriticalSection(); |
98 } | 84 } |
99 | 85 |
100 SSRCDatabase::~SSRCDatabase() | 86 SSRCDatabase::~SSRCDatabase() { |
101 { | 87 _ssrcMap.clear(); |
102 _ssrcMap.clear(); | 88 delete _critSect; |
103 delete _critSect; | |
104 } | 89 } |
105 | 90 |
106 uint32_t SSRCDatabase::GenerateRandom() | 91 uint32_t SSRCDatabase::GenerateRandom() { |
107 { | 92 uint32_t ssrc = 0; |
108 uint32_t ssrc = 0; | 93 do { |
109 do | 94 ssrc = rand(); |
110 { | 95 ssrc = ssrc << 16; |
111 ssrc = rand(); | 96 ssrc += rand(); |
112 ssrc = ssrc <<16; | 97 } while (ssrc == 0 || ssrc == 0xffffffff); |
113 ssrc += rand(); | |
114 | 98 |
115 } while (ssrc == 0 || ssrc == 0xffffffff); | 99 return ssrc; |
116 | |
117 return ssrc; | |
118 } | 100 } |
119 } // namespace webrtc | 101 } // namespace webrtc |
OLD | NEW |