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 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
| 14 | #include "webrtc/system_wrappers/include/clock.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | namespace webrtc { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 18 | namespace { |
| 19 | uint64_t Seed() { |
| 20 | return Clock::GetRealTimeClock()->TimeInMicroseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | } |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 22 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 24 | SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 25 | return GetStaticInstance<SSRCDatabase>(kAddRef); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 28 | void SSRCDatabase::ReturnSSRCDatabase() { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 29 | GetStaticInstance<SSRCDatabase>(kRelease); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 32 | uint32_t SSRCDatabase::CreateSSRC() { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 33 | CriticalSectionScoped lock(crit_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 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 | } |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 41 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 44 | void SSRCDatabase::RegisterSSRC(uint32_t ssrc) { |
| 45 | CriticalSectionScoped lock(crit_.get()); |
| 46 | ssrcs_.insert(ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 49 | void SSRCDatabase::ReturnSSRC(uint32_t ssrc) { |
| 50 | CriticalSectionScoped lock(crit_.get()); |
| 51 | ssrcs_.erase(ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 54 | SSRCDatabase::SSRCDatabase() |
| 55 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), random_(Seed()) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 57 | SSRCDatabase::~SSRCDatabase() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 60 | } // namespace webrtc |