xref: /aosp_15_r20/external/openscreen/cast/streaming/answer_messages.h (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 #ifndef CAST_STREAMING_ANSWER_MESSAGES_H_
6 #define CAST_STREAMING_ANSWER_MESSAGES_H_
7 
8 #include <array>
9 #include <chrono>
10 #include <cstdint>
11 #include <initializer_list>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "cast/streaming/resolution.h"
19 #include "cast/streaming/ssrc.h"
20 #include "json/value.h"
21 #include "platform/base/error.h"
22 #include "util/simple_fraction.h"
23 
24 namespace openscreen {
25 namespace cast {
26 
27 // For each of the below classes, though a number of methods are shared, the use
28 // of a shared base class has intentionally been avoided. This is to improve
29 // readability of the structs provided in this file by cutting down on the
30 // amount of obscuring boilerplate code. For each of the following struct
31 // definitions, the following method definitions are shared:
32 // (1) TryParse. Shall return a boolean indicating whether the out
33 //     parameter is in a valid state after checking bounds and restrictions.
34 // (2) ToJson. Should return a proper JSON object. Assumes that IsValid()
35 //     has been called already, OSP_DCHECKs if not IsValid().
36 // (3) IsValid. Used by both TryParse and ToJson to ensure that the
37 //     object is in a good state.
38 struct AudioConstraints {
39   static bool TryParse(const Json::Value& value, AudioConstraints* out);
40   Json::Value ToJson() const;
41   bool IsValid() const;
42 
43   int max_sample_rate = 0;
44   int max_channels = 0;
45   int min_bit_rate = 0;  // optional
46   int max_bit_rate = 0;
47   absl::optional<std::chrono::milliseconds> max_delay = {};
48 };
49 
50 struct VideoConstraints {
51   static bool TryParse(const Json::Value& value, VideoConstraints* out);
52   Json::Value ToJson() const;
53   bool IsValid() const;
54 
55   absl::optional<double> max_pixels_per_second = {};
56   absl::optional<Dimensions> min_resolution = {};
57   Dimensions max_dimensions = {};
58   int min_bit_rate = 0;  // optional
59   int max_bit_rate = 0;
60   absl::optional<std::chrono::milliseconds> max_delay = {};
61 };
62 
63 struct Constraints {
64   static bool TryParse(const Json::Value& value, Constraints* out);
65   Json::Value ToJson() const;
66   bool IsValid() const;
67 
68   AudioConstraints audio;
69   VideoConstraints video;
70 };
71 
72 // Decides whether the Sender scales and letterboxes content to 16:9, or if
73 // it may send video frames of any arbitrary size and the Receiver must
74 // handle the presentation details.
75 enum class AspectRatioConstraint : uint8_t { kVariable = 0, kFixed };
76 
77 struct AspectRatio {
78   static bool TryParse(const Json::Value& value, AspectRatio* out);
79   bool IsValid() const;
80 
81   bool operator==(const AspectRatio& other) const {
82     return width == other.width && height == other.height;
83   }
84 
85   int width = 0;
86   int height = 0;
87 };
88 
89 struct DisplayDescription {
90   static bool TryParse(const Json::Value& value, DisplayDescription* out);
91   Json::Value ToJson() const;
92   bool IsValid() const;
93 
94   // May exceed, be the same, or less than those mentioned in the
95   // video constraints.
96   absl::optional<Dimensions> dimensions;
97   absl::optional<AspectRatio> aspect_ratio = {};
98   absl::optional<AspectRatioConstraint> aspect_ratio_constraint = {};
99 };
100 
101 struct Answer {
102   // TODO(jophba): DEPRECATED, remove separately.
103   static bool ParseAndValidate(const Json::Value& value, Answer* out);
104 
105   static bool TryParse(const Json::Value& value, Answer* out);
106   Json::Value ToJson() const;
107   bool IsValid() const;
108 
109   int udp_port = 0;
110   std::vector<int> send_indexes;
111   std::vector<Ssrc> ssrcs;
112 
113   // Constraints and display descriptions are optional fields, and maybe null in
114   // the valid case.
115   absl::optional<Constraints> constraints;
116   absl::optional<DisplayDescription> display;
117   std::vector<int> receiver_rtcp_event_log;
118   std::vector<int> receiver_rtcp_dscp;
119 
120   // RTP extensions should be empty, but not null.
121   std::vector<std::string> rtp_extensions = {};
122 };
123 
124 }  // namespace cast
125 }  // namespace openscreen
126 
127 #endif  // CAST_STREAMING_ANSWER_MESSAGES_H_
128