blob: df09b01bdf298f92d2cd4ef8a8b4446d8bdfdb41 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
#include <assert.h>
#include <stdlib.h>
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#ifdef _WIN32
#include <windows.h>
#include <MMSystem.h> //timeGetTime
// TODO(hellner): investigate if it is necessary to disable these warnings.
#pragma warning(disable:4311)
#pragma warning(disable:4312)
#else
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#endif
namespace webrtc {
SSRCDatabase*
SSRCDatabase::StaticInstance(CountOperation count_operation)
{
SSRCDatabase* impl =
GetStaticInstance<SSRCDatabase>(count_operation);
return impl;
}
SSRCDatabase*
SSRCDatabase::GetSSRCDatabase()
{
return StaticInstance(kAddRef);
}
void
SSRCDatabase::ReturnSSRCDatabase()
{
StaticInstance(kRelease);
}
uint32_t
SSRCDatabase::CreateSSRC()
{
CriticalSectionScoped lock(_critSect);
uint32_t ssrc = GenerateRandom();
#ifndef WEBRTC_NO_STL
while(_ssrcMap.find(ssrc) != _ssrcMap.end())
{
ssrc = GenerateRandom();
}
_ssrcMap[ssrc] = 0;
#else
if(_sizeOfSSRC <= _numberOfSSRC)
{
// allocate more space
const int newSize = _sizeOfSSRC + 10;
uint32_t* tempSSRCVector = new uint32_t[newSize];
memcpy(tempSSRCVector, _ssrcVector, _sizeOfSSRC*sizeof(uint32_t));
delete [] _ssrcVector;
_ssrcVector = tempSSRCVector;
_sizeOfSSRC = newSize;
}
// check if in DB
if(_ssrcVector)
{
for (int i=0; i<_numberOfSSRC; i++)
{
if (_ssrcVector[i] == ssrc)
{
// we have a match
i = 0; // start over with a new ssrc
ssrc = GenerateRandom();
}
}
// add to database
_ssrcVector[_numberOfSSRC] = ssrc;
_numberOfSSRC++;
}
#endif
return ssrc;
}
int32_t
SSRCDatabase::RegisterSSRC(const uint32_t ssrc)
{
CriticalSectionScoped lock(_critSect);
#ifndef WEBRTC_NO_STL
_ssrcMap[ssrc] = 0;
#else
if(_sizeOfSSRC <= _numberOfSSRC)
{
// allocate more space
const int newSize = _sizeOfSSRC + 10;
uint32_t* tempSSRCVector = new uint32_t[newSize];
memcpy(tempSSRCVector, _ssrcVector, _sizeOfSSRC*sizeof(uint32_t));
delete [] _ssrcVector;
_ssrcVector = tempSSRCVector;
_sizeOfSSRC = newSize;
}
// check if in DB
if(_ssrcVector)
{
for (int i=0; i<_numberOfSSRC; i++)
{
if (_ssrcVector[i] == ssrc)
{
// we have a match
return -1;
}
}
// add to database
_ssrcVector[_numberOfSSRC] = ssrc;
_numberOfSSRC++;
}
#endif
return 0;
}
int32_t
SSRCDatabase::ReturnSSRC(const uint32_t ssrc)
{
CriticalSectionScoped lock(_critSect);
#ifndef WEBRTC_NO_STL
_ssrcMap.erase(ssrc);
#else
if(_ssrcVector)
{
for (int i=0; i<_numberOfSSRC; i++)
{
if (_ssrcVector[i] == ssrc)
{
// we have a match
// remove from database
_ssrcVector[i] = _ssrcVector[_numberOfSSRC-1];
_numberOfSSRC--;
break;
}
}
}
#endif
return 0;
}
SSRCDatabase::SSRCDatabase()
{
// we need to seed the random generator, otherwise we get 26500 each time, hardly a random value :)
#ifdef _WIN32
srand(timeGetTime());
#else
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
srand(tv.tv_usec);
#endif
#ifdef WEBRTC_NO_STL
_sizeOfSSRC = 10;
_numberOfSSRC = 0;
_ssrcVector = new uint32_t[10];
#endif
_critSect = CriticalSectionWrapper::CreateCriticalSection();
}
SSRCDatabase::~SSRCDatabase()
{
#ifdef WEBRTC_NO_STL
delete [] _ssrcVector;
#else
_ssrcMap.clear();
#endif
delete _critSect;
}
uint32_t SSRCDatabase::GenerateRandom()
{
uint32_t ssrc = 0;
do
{
ssrc = rand();
ssrc = ssrc <<16;
ssrc += rand();
} while (ssrc == 0 || ssrc == 0xffffffff);
return ssrc;
}
} // namespace webrtc
|