blob: c9572cd1b7c26ae6617031043a521a55f2b7a7c7 [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
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdlib.h>
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000015
Henrik Kjellander98f53512015-10-28 18:17:40 +010016#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18#ifdef _WIN32
danilchap162abd32015-12-10 02:39:40 -080019#include <windows.h>
20#include <MMSystem.h> // timeGetTime
niklase@google.com470e71d2011-07-07 08:21:25 +000021
henrike@webrtc.org315282c2011-12-09 17:46:20 +000022// TODO(hellner): investigate if it is necessary to disable these warnings.
danilchap162abd32015-12-10 02:39:40 -080023#pragma warning(disable : 4311)
24#pragma warning(disable : 4312)
niklase@google.com470e71d2011-07-07 08:21:25 +000025#else
danilchap162abd32015-12-10 02:39:40 -080026#include <stdio.h>
27#include <string.h>
28#include <time.h>
29#include <sys/time.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000030#endif
31
32namespace webrtc {
danilchap162abd32015-12-10 02:39:40 -080033SSRCDatabase* SSRCDatabase::StaticInstance(CountOperation count_operation) {
34 SSRCDatabase* impl = GetStaticInstance<SSRCDatabase>(count_operation);
henrike@webrtc.org315282c2011-12-09 17:46:20 +000035 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
danilchap162abd32015-12-10 02:39:40 -080038SSRCDatabase* SSRCDatabase::GetSSRCDatabase() {
39 return StaticInstance(kAddRef);
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
danilchap162abd32015-12-10 02:39:40 -080042void SSRCDatabase::ReturnSSRCDatabase() {
43 StaticInstance(kRelease);
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
danilchap162abd32015-12-10 02:39:40 -080046uint32_t SSRCDatabase::CreateSSRC() {
47 CriticalSectionScoped lock(_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000048
danilchap162abd32015-12-10 02:39:40 -080049 uint32_t ssrc = GenerateRandom();
niklase@google.com470e71d2011-07-07 08:21:25 +000050
danilchap162abd32015-12-10 02:39:40 -080051 while (_ssrcMap.find(ssrc) != _ssrcMap.end()) {
52 ssrc = GenerateRandom();
53 }
54 _ssrcMap[ssrc] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000055
danilchap162abd32015-12-10 02:39:40 -080056 return ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
danilchap162abd32015-12-10 02:39:40 -080059int32_t SSRCDatabase::RegisterSSRC(const uint32_t ssrc) {
60 CriticalSectionScoped lock(_critSect);
61 _ssrcMap[ssrc] = 0;
62 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
danilchap162abd32015-12-10 02:39:40 -080065int32_t SSRCDatabase::ReturnSSRC(const uint32_t ssrc) {
66 CriticalSectionScoped lock(_critSect);
67 _ssrcMap.erase(ssrc);
68 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
danilchap162abd32015-12-10 02:39:40 -080071SSRCDatabase::SSRCDatabase() {
72// we need to seed the random generator, otherwise we get 26500 each time,
73// hardly a random value :)
niklase@google.com470e71d2011-07-07 08:21:25 +000074#ifdef _WIN32
danilchap162abd32015-12-10 02:39:40 -080075 srand(timeGetTime());
niklase@google.com470e71d2011-07-07 08:21:25 +000076#else
danilchap162abd32015-12-10 02:39:40 -080077 struct timeval tv;
78 struct timezone tz;
79 gettimeofday(&tv, &tz);
80 srand(tv.tv_usec);
niklase@google.com470e71d2011-07-07 08:21:25 +000081#endif
82
danilchap162abd32015-12-10 02:39:40 -080083 _critSect = CriticalSectionWrapper::CreateCriticalSection();
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
danilchap162abd32015-12-10 02:39:40 -080086SSRCDatabase::~SSRCDatabase() {
87 _ssrcMap.clear();
88 delete _critSect;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
danilchap162abd32015-12-10 02:39:40 -080091uint32_t SSRCDatabase::GenerateRandom() {
92 uint32_t ssrc = 0;
93 do {
94 ssrc = rand();
95 ssrc = ssrc << 16;
96 ssrc += rand();
97 } while (ssrc == 0 || ssrc == 0xffffffff);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
danilchap162abd32015-12-10 02:39:40 -080099 return ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000101} // namespace webrtc