blob: f1d1549e279d5ea47ddae621bdf3407dfdcf4bfe [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.orga048d7c2013-05-29 14:27:38 +000011#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
danilchape005cf22015-12-15 01:59:47 -080013#include "webrtc/base/checks.h"
tommiae695e92016-02-02 08:31:45 -080014#include "webrtc/system_wrappers/include/tick_util.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
niklase@google.com470e71d2011-07-07 08:21:25 +000016namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000017
danilchap162abd32015-12-10 02:39:40 -080018SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
danilchape005cf22015-12-15 01:59:47 -080019 return GetStaticInstance<SSRCDatabase>(kAddRef);
niklase@google.com470e71d2011-07-07 08:21:25 +000020}
21
danilchap162abd32015-12-10 02:39:40 -080022void SSRCDatabase::ReturnSSRCDatabase() {
danilchape005cf22015-12-15 01:59:47 -080023 GetStaticInstance<SSRCDatabase>(kRelease);
niklase@google.com470e71d2011-07-07 08:21:25 +000024}
25
danilchap162abd32015-12-10 02:39:40 -080026uint32_t SSRCDatabase::CreateSSRC() {
tommiae695e92016-02-02 08:31:45 -080027 rtc::CritScope lock(&crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +000028
danilchape005cf22015-12-15 01:59:47 -080029 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 }
danilchap162abd32015-12-10 02:39:40 -080035 }
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
danilchape005cf22015-12-15 01:59:47 -080038void SSRCDatabase::RegisterSSRC(uint32_t ssrc) {
tommiae695e92016-02-02 08:31:45 -080039 rtc::CritScope lock(&crit_);
danilchape005cf22015-12-15 01:59:47 -080040 ssrcs_.insert(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
danilchape005cf22015-12-15 01:59:47 -080043void SSRCDatabase::ReturnSSRC(uint32_t ssrc) {
tommiae695e92016-02-02 08:31:45 -080044 rtc::CritScope lock(&crit_);
danilchape005cf22015-12-15 01:59:47 -080045 ssrcs_.erase(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
tommiae695e92016-02-02 08:31:45 -080048SSRCDatabase::SSRCDatabase() : random_(TickTime::Now().Ticks()) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000049
tommiae695e92016-02-02 08:31:45 -080050SSRCDatabase::~SSRCDatabase() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000051
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000052} // namespace webrtc