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