niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 15 | |
| 16 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef _WIN32 |
| 19 | #include <windows.h> |
| 20 | #include <MMSystem.h> //timeGetTime |
| 21 | |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 22 | // TODO(hellner): investigate if it is necessary to disable these warnings. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #pragma warning(disable:4311) |
| 24 | #pragma warning(disable:4312) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | #else |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <sys/time.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | namespace webrtc { |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 33 | SSRCDatabase* |
| 34 | SSRCDatabase::StaticInstance(CountOperation count_operation) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | { |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 36 | SSRCDatabase* impl = |
| 37 | GetStaticInstance<SSRCDatabase>(count_operation); |
| 38 | return impl; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | SSRCDatabase* |
| 42 | SSRCDatabase::GetSSRCDatabase() |
| 43 | { |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 44 | return StaticInstance(kAddRef); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | SSRCDatabase::ReturnSSRCDatabase() |
| 49 | { |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 50 | StaticInstance(kRelease); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 53 | uint32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | SSRCDatabase::CreateSSRC() |
| 55 | { |
henrike@webrtc.org | 65573f2 | 2011-12-13 19:17:27 +0000 | [diff] [blame] | 56 | CriticalSectionScoped lock(_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 58 | uint32_t ssrc = GenerateRandom(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 60 | #ifndef WEBRTC_NO_STL |
| 61 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | while(_ssrcMap.find(ssrc) != _ssrcMap.end()) |
| 63 | { |
| 64 | ssrc = GenerateRandom(); |
| 65 | } |
| 66 | _ssrcMap[ssrc] = 0; |
| 67 | |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 68 | #else |
| 69 | if(_sizeOfSSRC <= _numberOfSSRC) |
| 70 | { |
| 71 | // allocate more space |
| 72 | const int newSize = _sizeOfSSRC + 10; |
| 73 | uint32_t* tempSSRCVector = new uint32_t[newSize]; |
| 74 | memcpy(tempSSRCVector, _ssrcVector, _sizeOfSSRC*sizeof(uint32_t)); |
| 75 | delete [] _ssrcVector; |
| 76 | |
| 77 | _ssrcVector = tempSSRCVector; |
| 78 | _sizeOfSSRC = newSize; |
| 79 | } |
| 80 | |
| 81 | // check if in DB |
| 82 | if(_ssrcVector) |
| 83 | { |
| 84 | for (int i=0; i<_numberOfSSRC; i++) |
| 85 | { |
| 86 | if (_ssrcVector[i] == ssrc) |
| 87 | { |
| 88 | // we have a match |
| 89 | i = 0; // start over with a new ssrc |
| 90 | ssrc = GenerateRandom(); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | // add to database |
| 95 | _ssrcVector[_numberOfSSRC] = ssrc; |
| 96 | _numberOfSSRC++; |
| 97 | } |
| 98 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | return ssrc; |
| 100 | } |
| 101 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 102 | int32_t |
| 103 | SSRCDatabase::RegisterSSRC(const uint32_t ssrc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | { |
henrike@webrtc.org | 65573f2 | 2011-12-13 19:17:27 +0000 | [diff] [blame] | 105 | CriticalSectionScoped lock(_critSect); |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 106 | |
| 107 | #ifndef WEBRTC_NO_STL |
| 108 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | _ssrcMap[ssrc] = 0; |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 110 | |
| 111 | #else |
| 112 | if(_sizeOfSSRC <= _numberOfSSRC) |
| 113 | { |
| 114 | // allocate more space |
| 115 | const int newSize = _sizeOfSSRC + 10; |
| 116 | uint32_t* tempSSRCVector = new uint32_t[newSize]; |
| 117 | memcpy(tempSSRCVector, _ssrcVector, _sizeOfSSRC*sizeof(uint32_t)); |
| 118 | delete [] _ssrcVector; |
| 119 | |
| 120 | _ssrcVector = tempSSRCVector; |
| 121 | _sizeOfSSRC = newSize; |
| 122 | } |
| 123 | // check if in DB |
| 124 | if(_ssrcVector) |
| 125 | { |
| 126 | for (int i=0; i<_numberOfSSRC; i++) |
| 127 | { |
| 128 | if (_ssrcVector[i] == ssrc) |
| 129 | { |
| 130 | // we have a match |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | // add to database |
| 135 | _ssrcVector[_numberOfSSRC] = ssrc; |
| 136 | _numberOfSSRC++; |
| 137 | } |
| 138 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 142 | int32_t |
| 143 | SSRCDatabase::ReturnSSRC(const uint32_t ssrc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | { |
henrike@webrtc.org | 65573f2 | 2011-12-13 19:17:27 +0000 | [diff] [blame] | 145 | CriticalSectionScoped lock(_critSect); |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 146 | |
| 147 | #ifndef WEBRTC_NO_STL |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | _ssrcMap.erase(ssrc); |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 149 | |
| 150 | #else |
| 151 | if(_ssrcVector) |
| 152 | { |
| 153 | for (int i=0; i<_numberOfSSRC; i++) |
| 154 | { |
| 155 | if (_ssrcVector[i] == ssrc) |
| 156 | { |
| 157 | // we have a match |
| 158 | // remove from database |
| 159 | _ssrcVector[i] = _ssrcVector[_numberOfSSRC-1]; |
| 160 | _numberOfSSRC--; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | SSRCDatabase::SSRCDatabase() |
| 170 | { |
| 171 | // we need to seed the random generator, otherwise we get 26500 each time, hardly a random value :) |
| 172 | #ifdef _WIN32 |
| 173 | srand(timeGetTime()); |
| 174 | #else |
| 175 | struct timeval tv; |
| 176 | struct timezone tz; |
| 177 | gettimeofday(&tv, &tz); |
| 178 | srand(tv.tv_usec); |
| 179 | #endif |
| 180 | |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 181 | #ifdef WEBRTC_NO_STL |
| 182 | _sizeOfSSRC = 10; |
| 183 | _numberOfSSRC = 0; |
| 184 | _ssrcVector = new uint32_t[10]; |
| 185 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | _critSect = CriticalSectionWrapper::CreateCriticalSection(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | SSRCDatabase::~SSRCDatabase() |
| 190 | { |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 191 | #ifdef WEBRTC_NO_STL |
| 192 | delete [] _ssrcVector; |
| 193 | #else |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | _ssrcMap.clear(); |
andresp@webrtc.org | 5ac876b | 2014-07-10 07:41:59 +0000 | [diff] [blame^] | 195 | #endif |
| 196 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | } |
| 198 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 199 | uint32_t SSRCDatabase::GenerateRandom() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 201 | uint32_t ssrc = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | do |
| 203 | { |
| 204 | ssrc = rand(); |
| 205 | ssrc = ssrc <<16; |
| 206 | ssrc += rand(); |
| 207 | |
| 208 | } while (ssrc == 0 || ssrc == 0xffffffff); |
| 209 | |
| 210 | return ssrc; |
| 211 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 212 | } // namespace webrtc |