blob: ac84107eda108249d94d31a8717763e9f04af178 [file] [log] [blame]
Karl Wiberg98900742018-03-01 12:03:49 +01001/*
2 * Copyright (c) 2018 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
11#include "api/audio_codecs/audio_codec_pair_id.h"
12
13#include <atomic>
14
15#include "rtc_base/checks.h"
16
17namespace webrtc {
18
19namespace {
20
21// Returns a new value that it has never returned before. You may call it at
22// most 2^63 times in the lifetime of the program. Note: The returned values
23// may be easily predictable.
24uint64_t GetNextId() {
25 static std::atomic<uint64_t> next_id(0);
26
27 // Atomically increment `next_id`, and return the previous value. Relaxed
28 // memory order is sufficient, since all we care about is that different
29 // callers return different values.
30 const uint64_t new_id = next_id.fetch_add(1, std::memory_order_relaxed);
31
32 // This check isn't atomic with the increment, so if we start 2^63 + 1
33 // invocations of GetNextId() in parallel, the last one to do the atomic
34 // increment could return the ID 0 before any of the others had time to
35 // trigger this DCHECK. We blithely assume that this won't happen.
36 RTC_DCHECK_LT(new_id, uint64_t{1} << 63) << "Used up all ID values";
37
38 return new_id;
39}
40
41// Make an integer ID more unpredictable. This is a 1:1 mapping, so you can
42// feed it any value, but the idea is that you can feed it a sequence such as
43// 0, 1, 2, ... and get a new sequence that isn't as trivially predictable, so
44// that users won't rely on it being consecutive or increasing or anything like
45// that.
46constexpr uint64_t ObfuscateId(uint64_t id) {
47 // Any nonzero coefficient that's relatively prime to 2^64 (that is, any odd
48 // number) and any constant will give a 1:1 mapping. These high-entropy
49 // values will prevent the sequence from being trivially predictable.
50 //
51 // Both the multiplication and the addition going to overflow almost always,
52 // but that's fine---we *want* arithmetic mod 2^64.
53 return uint64_t{0x85fdb20e1294309a} + uint64_t{0xc516ef5c37462469} * id;
54}
55
56// The first ten values. Verified against the Python function
57//
58// def f(n):
59// return (0x85fdb20e1294309a + 0xc516ef5c37462469 * n) % 2**64
60//
61// Callers should obviously not depend on these exact values...
62//
63// (On Visual C++, we have to disable warning C4307 (integral constant
64// overflow), even though unsigned integers have perfectly well-defined
65// overflow behavior.)
66#ifdef _MSC_VER
67#pragma warning(push)
68#pragma warning(disable : 4307)
69#endif
70static_assert(ObfuscateId(0) == uint64_t{0x85fdb20e1294309a}, "");
71static_assert(ObfuscateId(1) == uint64_t{0x4b14a16a49da5503}, "");
72static_assert(ObfuscateId(2) == uint64_t{0x102b90c68120796c}, "");
73static_assert(ObfuscateId(3) == uint64_t{0xd5428022b8669dd5}, "");
74static_assert(ObfuscateId(4) == uint64_t{0x9a596f7eefacc23e}, "");
75static_assert(ObfuscateId(5) == uint64_t{0x5f705edb26f2e6a7}, "");
76static_assert(ObfuscateId(6) == uint64_t{0x24874e375e390b10}, "");
77static_assert(ObfuscateId(7) == uint64_t{0xe99e3d93957f2f79}, "");
78static_assert(ObfuscateId(8) == uint64_t{0xaeb52cefccc553e2}, "");
79static_assert(ObfuscateId(9) == uint64_t{0x73cc1c4c040b784b}, "");
80#ifdef _MSC_VER
81#pragma warning(pop)
82#endif
83
84} // namespace
85
86AudioCodecPairId AudioCodecPairId::Create() {
87 return AudioCodecPairId(ObfuscateId(GetNextId()));
88}
89
90} // namespace webrtc