Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: webrtc/base/helpers.cc

Issue 1413713003: Adding the ability to create an RtpSender without a track. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Adding some unit tests for new methods on the sender. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 return true; 157 return true;
158 } 158 }
159 159
160 private: 160 private:
161 int GetRandom() { 161 int GetRandom() {
162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; 162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff;
163 } 163 }
164 int seed_; 164 int seed_;
165 }; 165 };
166 166
167 // TODO: Use Base64::Base64Table instead.
168 static const char BASE64[64] = {
169 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
170 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
171 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
172 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
173 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
174 };
175
176 namespace { 167 namespace {
177 168
169 // TODO: Use Base64::Base64Table instead.
170 static const char kBase64[64] = {
171 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
172 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
173 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
174 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
175 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
176
177 static const char kHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
178 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
179
178 // This round about way of creating a global RNG is to safe-guard against 180 // This round about way of creating a global RNG is to safe-guard against
179 // indeterminant static initialization order. 181 // indeterminant static initialization order.
180 scoped_ptr<RandomGenerator>& GetGlobalRng() { 182 scoped_ptr<RandomGenerator>& GetGlobalRng() {
181 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, 183 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng,
182 (new SecureRandomGenerator())); 184 (new SecureRandomGenerator()));
183 return global_rng; 185 return global_rng;
184 } 186 }
185 187
186 RandomGenerator& Rng() { 188 RandomGenerator& Rng() {
187 return *GetGlobalRng(); 189 return *GetGlobalRng();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return false; 227 return false;
226 } 228 }
227 str->reserve(len); 229 str->reserve(len);
228 for (size_t i = 0; i < len; ++i) { 230 for (size_t i = 0; i < len; ++i) {
229 str->push_back(table[bytes[i] % table_size]); 231 str->push_back(table[bytes[i] % table_size]);
230 } 232 }
231 return true; 233 return true;
232 } 234 }
233 235
234 bool CreateRandomString(size_t len, std::string* str) { 236 bool CreateRandomString(size_t len, std::string* str) {
235 return CreateRandomString(len, BASE64, 64, str); 237 return CreateRandomString(len, kBase64, 64, str);
236 } 238 }
237 239
238 bool CreateRandomString(size_t len, const std::string& table, 240 bool CreateRandomString(size_t len, const std::string& table,
239 std::string* str) { 241 std::string* str) {
240 return CreateRandomString(len, table.c_str(), 242 return CreateRandomString(len, table.c_str(),
241 static_cast<int>(table.size()), str); 243 static_cast<int>(table.size()), str);
242 } 244 }
243 245
246 // Version 4 UUID is of the form:
247 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
248 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b.
249 std::string CreateRandomUuid() {
250 std::string str;
251 scoped_ptr<uint8_t[]> bytes(new uint8_t[31]);
252 if (!Rng().Generate(bytes.get(), 31)) {
253 LOG(LS_ERROR) << "Failed to generate random string!";
254 return str;
255 }
256 str.reserve(36);
257 for (size_t i = 0; i < 8; ++i) {
258 str.push_back(kHex[bytes[i] % 16]);
259 }
260 str.push_back('-');
261 for (size_t i = 8; i < 12; ++i) {
262 str.push_back(kHex[bytes[i] % 16]);
263 }
264 str.push_back('-');
265 str.push_back('4');
266 for (size_t i = 12; i < 15; ++i) {
267 str.push_back(kHex[bytes[i] % 16]);
268 }
269 str.push_back('-');
270 str.push_back(kHex[((bytes[15] & ~0x4) | 0x8) % 16]);
pthatcher1 2015/10/20 17:42:50 Could you make a table with 8, 9, a, and b and the
Taylor Brandstetter 2015/10/21 00:22:09 Done.
271 for (size_t i = 16; i < 19; ++i) {
272 str.push_back(kHex[bytes[i] % 16]);
273 }
274 str.push_back('-');
275 for (size_t i = 19; i < 31; ++i) {
276 str.push_back(kHex[bytes[i] % 16]);
277 }
278 return str;
279 }
280
244 uint32_t CreateRandomId() { 281 uint32_t CreateRandomId() {
245 uint32_t id; 282 uint32_t id;
246 if (!Rng().Generate(&id, sizeof(id))) { 283 if (!Rng().Generate(&id, sizeof(id))) {
247 LOG(LS_ERROR) << "Failed to generate random id!"; 284 LOG(LS_ERROR) << "Failed to generate random id!";
248 } 285 }
249 return id; 286 return id;
250 } 287 }
251 288
252 uint64_t CreateRandomId64() { 289 uint64_t CreateRandomId64() {
253 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); 290 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId();
254 } 291 }
255 292
256 uint32_t CreateRandomNonZeroId() { 293 uint32_t CreateRandomNonZeroId() {
257 uint32_t id; 294 uint32_t id;
258 do { 295 do {
259 id = CreateRandomId(); 296 id = CreateRandomId();
260 } while (id == 0); 297 } while (id == 0);
261 return id; 298 return id;
262 } 299 }
263 300
264 double CreateRandomDouble() { 301 double CreateRandomDouble() {
265 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + 302 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() +
266 std::numeric_limits<double>::epsilon()); 303 std::numeric_limits<double>::epsilon());
267 } 304 }
268 305
269 } // namespace rtc 306 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698