blob: fb02b7ef12cd12f785fd18ed331e1407d3fd38c4 [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"
14#include "webrtc/system_wrappers/include/clock.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
niklase@google.com470e71d2011-07-07 08:21:25 +000017namespace webrtc {
danilchape005cf22015-12-15 01:59:47 -080018namespace {
19uint64_t Seed() {
20 return Clock::GetRealTimeClock()->TimeInMicroseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000021}
danilchape005cf22015-12-15 01:59:47 -080022} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000023
danilchap162abd32015-12-10 02:39:40 -080024SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
danilchape005cf22015-12-15 01:59:47 -080025 return GetStaticInstance<SSRCDatabase>(kAddRef);
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
danilchap162abd32015-12-10 02:39:40 -080028void SSRCDatabase::ReturnSSRCDatabase() {
danilchape005cf22015-12-15 01:59:47 -080029 GetStaticInstance<SSRCDatabase>(kRelease);
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
danilchap162abd32015-12-10 02:39:40 -080032uint32_t SSRCDatabase::CreateSSRC() {
danilchape005cf22015-12-15 01:59:47 -080033 CriticalSectionScoped lock(crit_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +000034
danilchape005cf22015-12-15 01:59:47 -080035 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 }
danilchap162abd32015-12-10 02:39:40 -080041 }
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
danilchape005cf22015-12-15 01:59:47 -080044void SSRCDatabase::RegisterSSRC(uint32_t ssrc) {
45 CriticalSectionScoped lock(crit_.get());
46 ssrcs_.insert(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
danilchape005cf22015-12-15 01:59:47 -080049void SSRCDatabase::ReturnSSRC(uint32_t ssrc) {
50 CriticalSectionScoped lock(crit_.get());
51 ssrcs_.erase(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
danilchape005cf22015-12-15 01:59:47 -080054SSRCDatabase::SSRCDatabase()
55 : crit_(CriticalSectionWrapper::CreateCriticalSection()), random_(Seed()) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000056
danilchap162abd32015-12-10 02:39:40 -080057SSRCDatabase::~SSRCDatabase() {
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000060} // namespace webrtc