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 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef _WIN32 |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 19 | #include <windows.h> |
| 20 | #include <MMSystem.h> // timeGetTime |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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. |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [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 |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 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 { |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 33 | SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) { |
| 34 | SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation); |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 35 | return impl; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 38 | SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { |
| 39 | return StaticInstance(kAddRef); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 42 | void SSRCDatabase::ReturnSSRCDatabase() { |
| 43 | StaticInstance(kRelease); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 46 | uint32_t SSRCDatabase::CreateSSRC() { |
| 47 | CriticalSectionScoped lock(_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 49 | uint32_t ssrc = GenerateRandom(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 51 | while (_ssrcMap.find(ssrc) != _ssrcMap.end()) { |
| 52 | ssrc = GenerateRandom(); |
| 53 | } |
| 54 | _ssrcMap[ssrc] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 56 | return ssrc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 59 | int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) { |
| 60 | CriticalSectionScoped lock(_critSect); |
| 61 | _ssrcMap[ssrc] = 0; |
| 62 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 65 | int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) { |
| 66 | CriticalSectionScoped lock(_critSect); |
| 67 | _ssrcMap.erase(ssrc); |
| 68 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 71 | SSRCDatabase::SSRCDatabase() { |
| 72 | // we need to seed the random generator, otherwise we get 26500 each time, |
| 73 | // hardly a random value :) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | #ifdef _WIN32 |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 75 | srand(timeGetTime()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | #else |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 77 | struct timeval tv; |
| 78 | struct timezone tz; |
| 79 | gettimeofday(&tv, &tz); |
| 80 | srand(tv.tv_usec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 83 | _critSect = CriticalSectionWrapper::CreateCriticalSection(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 86 | SSRCDatabase::~SSRCDatabase() { |
| 87 | _ssrcMap.clear(); |
| 88 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 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); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 99 | return ssrc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 101 | } // namespace webrtc |