blob: 1594a17a4a52ae0dab45c49e8b8a352421cc2734 [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
16#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18#ifdef _WIN32
19 #include <windows.h>
20 #include <MMSystem.h> //timeGetTime
21
henrike@webrtc.org315282c2011-12-09 17:46:20 +000022// TODO(hellner): investigate if it is necessary to disable these warnings.
niklase@google.com470e71d2011-07-07 08:21:25 +000023 #pragma warning(disable:4311)
24 #pragma warning(disable:4312)
niklase@google.com470e71d2011-07-07 08:21:25 +000025#else
26 #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 {
henrike@webrtc.org315282c2011-12-09 17:46:20 +000033SSRCDatabase*
34SSRCDatabase::StaticInstance(CountOperation count_operation)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
henrike@webrtc.org315282c2011-12-09 17:46:20 +000036 SSRCDatabase* impl =
37 GetStaticInstance<SSRCDatabase>(count_operation);
38 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
41SSRCDatabase*
42SSRCDatabase::GetSSRCDatabase()
43{
henrike@webrtc.org315282c2011-12-09 17:46:20 +000044 return StaticInstance(kAddRef);
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
47void
48SSRCDatabase::ReturnSSRCDatabase()
49{
henrike@webrtc.org315282c2011-12-09 17:46:20 +000050 StaticInstance(kRelease);
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
pbos@webrtc.org2f446732013-04-08 11:08:41 +000053uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +000054SSRCDatabase::CreateSSRC()
55{
henrike@webrtc.org65573f22011-12-13 19:17:27 +000056 CriticalSectionScoped lock(_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
pbos@webrtc.org2f446732013-04-08 11:08:41 +000058 uint32_t ssrc = GenerateRandom();
niklase@google.com470e71d2011-07-07 08:21:25 +000059
niklase@google.com470e71d2011-07-07 08:21:25 +000060 while(_ssrcMap.find(ssrc) != _ssrcMap.end())
61 {
62 ssrc = GenerateRandom();
63 }
64 _ssrcMap[ssrc] = 0;
65
niklase@google.com470e71d2011-07-07 08:21:25 +000066 return ssrc;
67}
68
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069int32_t
70SSRCDatabase::RegisterSSRC(const uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +000071{
henrike@webrtc.org65573f22011-12-13 19:17:27 +000072 CriticalSectionScoped lock(_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000073 _ssrcMap[ssrc] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000074 return 0;
75}
76
pbos@webrtc.org2f446732013-04-08 11:08:41 +000077int32_t
78SSRCDatabase::ReturnSSRC(const uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +000079{
henrike@webrtc.org65573f22011-12-13 19:17:27 +000080 CriticalSectionScoped lock(_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000081 _ssrcMap.erase(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000082 return 0;
83}
84
85SSRCDatabase::SSRCDatabase()
86{
87 // we need to seed the random generator, otherwise we get 26500 each time, hardly a random value :)
88#ifdef _WIN32
89 srand(timeGetTime());
90#else
91 struct timeval tv;
92 struct timezone tz;
93 gettimeofday(&tv, &tz);
94 srand(tv.tv_usec);
95#endif
96
niklase@google.com470e71d2011-07-07 08:21:25 +000097 _critSect = CriticalSectionWrapper::CreateCriticalSection();
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
100SSRCDatabase::~SSRCDatabase()
101{
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 _ssrcMap.clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000103}
104
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000105uint32_t SSRCDatabase::GenerateRandom()
niklase@google.com470e71d2011-07-07 08:21:25 +0000106{
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107 uint32_t ssrc = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 do
109 {
110 ssrc = rand();
111 ssrc = ssrc <<16;
112 ssrc += rand();
113
114 } while (ssrc == 0 || ssrc == 0xffffffff);
115
116 return ssrc;
117}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000118} // namespace webrtc