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