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