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" |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/include/tick_util.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 18 | SSRCDatabase* SSRCDatabase::GetSSRCDatabase() { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 19 | return GetStaticInstance<SSRCDatabase>(kAddRef); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | } |
| 21 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 22 | void SSRCDatabase::ReturnSSRCDatabase() { |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 23 | GetStaticInstance<SSRCDatabase>(kRelease); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | } |
| 25 | |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 26 | uint32_t SSRCDatabase::CreateSSRC() { |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 27 | rtc::CritScope lock(&crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 29 | while (true) { // Try until get a new ssrc. |
| 30 | // 0 and 0xffffffff are invalid values for SSRC. |
| 31 | uint32_t ssrc = random_.Rand(1u, 0xfffffffe); |
| 32 | if (ssrcs_.insert(ssrc).second) { |
| 33 | return ssrc; |
| 34 | } |
danilchap | 162abd3 | 2015-12-10 02:39:40 -0800 | [diff] [blame] | 35 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 38 | void SSRCDatabase::RegisterSSRC(uint32_t ssrc) { |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 39 | rtc::CritScope lock(&crit_); |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 40 | ssrcs_.insert(ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 43 | void SSRCDatabase::ReturnSSRC(uint32_t ssrc) { |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 44 | rtc::CritScope lock(&crit_); |
danilchap | e005cf2 | 2015-12-15 01:59:47 -0800 | [diff] [blame] | 45 | ssrcs_.erase(ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 48 | SSRCDatabase::SSRCDatabase() : random_(TickTime::Now().Ticks()) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 50 | SSRCDatabase::~SSRCDatabase() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 52 | } // namespace webrtc |