xref: /aosp_15_r20/external/openscreen/cast/streaming/ssrc.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cast/streaming/ssrc.h"
6 
7 #include <random>
8 
9 #include "platform/api/time.h"
10 
11 namespace openscreen {
12 namespace cast {
13 
14 namespace {
15 
16 // These ranges are arbitrary, but have been used for several years (in prior
17 // implementations of Cast Streaming).
18 constexpr int kHigherPriorityMin = 1;
19 constexpr int kHigherPriorityMax = 50000;
20 constexpr int kNormalPriorityMin = 50001;
21 constexpr int kNormalPriorityMax = 100000;
22 
23 }  // namespace
24 
GenerateSsrc(bool higher_priority)25 Ssrc GenerateSsrc(bool higher_priority) {
26   // Use a statically-allocated generator, instantiated upon first use, and
27   // seeded with the current time tick count. This generator was chosen because
28   // it is light-weight and does not need to produce unguessable (nor
29   // crypto-secure) values.
30   static std::minstd_rand generator(static_cast<std::minstd_rand::result_type>(
31       Clock::now().time_since_epoch().count()));
32 
33   std::uniform_int_distribution<int> distribution(
34       higher_priority ? kHigherPriorityMin : kNormalPriorityMin,
35       higher_priority ? kHigherPriorityMax : kNormalPriorityMax);
36   return static_cast<Ssrc>(distribution(generator));
37 }
38 
ComparePriority(Ssrc ssrc_a,Ssrc ssrc_b)39 int ComparePriority(Ssrc ssrc_a, Ssrc ssrc_b) {
40   return static_cast<int>(ssrc_a) - static_cast<int>(ssrc_b);
41 }
42 
43 }  // namespace cast
44 }  // namespace openscreen
45