xref: /aosp_15_r20/external/openscreen/cast/streaming/resolution.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2021 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 // Resolutions and dimensions (resolutions with a frame rate) are used
6 // extensively throughout cast streaming. Since their serialization to and
7 // from JSON is stable and standard, we have a single place definition for
8 // these for use both in our public APIs and private messages.
9 
10 #ifndef CAST_STREAMING_RESOLUTION_H_
11 #define CAST_STREAMING_RESOLUTION_H_
12 
13 #include "absl/types/optional.h"
14 #include "json/value.h"
15 #include "util/simple_fraction.h"
16 
17 namespace openscreen {
18 namespace cast {
19 
20 // A resolution in pixels.
21 struct Resolution {
22   static bool TryParse(const Json::Value& value, Resolution* out);
23   bool IsValid() const;
24   Json::Value ToJson() const;
25 
26   // Returns true if both |width| and |height| of this instance are greater than
27   // or equal to that of |other|.
28   bool IsSupersetOf(const Resolution& other) const;
29 
30   bool operator==(const Resolution& other) const;
31   bool operator!=(const Resolution& other) const;
32 
33   // Width and height in pixels.
34   int width = 0;
35   int height = 0;
36 };
37 
38 // A resolution in pixels and a frame rate.
39 struct Dimensions {
40   static bool TryParse(const Json::Value& value, Dimensions* out);
41   bool IsValid() const;
42   Json::Value ToJson() const;
43 
44   // Returns true if all properties of this instance are greater than or equal
45   // to those of |other|.
46   bool IsSupersetOf(const Dimensions& other) const;
47 
48   bool operator==(const Dimensions& other) const;
49   bool operator!=(const Dimensions& other) const;
50 
51   // Get just the width and height fields (for comparisons).
ToResolutionDimensions52   constexpr Resolution ToResolution() const { return {width, height}; }
53 
54   // The effective bit rate is the width * height * frame rate.
effective_bit_rateDimensions55   constexpr int effective_bit_rate() const {
56     return width * height * static_cast<double>(frame_rate);
57   }
58 
59   // Width and height in pixels.
60   int width = 0;
61   int height = 0;
62 
63   // |frame_rate| is the maximum maintainable frame rate.
64   SimpleFraction frame_rate{0, 1};
65 };
66 
67 }  // namespace cast
68 }  // namespace openscreen
69 
70 #endif  // CAST_STREAMING_RESOLUTION_H_
71