aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/answer_messages.cc
blob: 2660ccd8b1ad85e2330ca8537e5a0f7610b3c4c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/streaming/answer_messages.h"

#include <utility>

#include "absl/strings/str_cat.h"
#include "cast/streaming/message_util.h"
#include "platform/base/error.h"
#include "util/logging.h"

namespace openscreen {
namespace cast {

namespace {

static constexpr char kMessageKeyType[] = "type";
static constexpr char kMessageTypeAnswer[] = "ANSWER";

// List of ANSWER message fields.
static constexpr char kAnswerMessageBody[] = "answer";
static constexpr char kResult[] = "result";
static constexpr char kResultOk[] = "ok";
static constexpr char kResultError[] = "error";
static constexpr char kErrorMessageBody[] = "error";
static constexpr char kErrorCode[] = "code";
static constexpr char kErrorDescription[] = "description";

Json::Value AspectRatioConstraintToJson(AspectRatioConstraint aspect_ratio) {
  switch (aspect_ratio) {
    case AspectRatioConstraint::kVariable:
      return Json::Value("receiver");
    case AspectRatioConstraint::kFixed:
    default:
      return Json::Value("sender");
  }
}

template <typename T>
Json::Value PrimitiveVectorToJson(const std::vector<T>& vec) {
  Json::Value array(Json::ValueType::arrayValue);
  array.resize(vec.size());

  for (Json::Value::ArrayIndex i = 0; i < vec.size(); ++i) {
    array[i] = Json::Value(vec[i]);
  }

  return array;
}

}  // namespace

ErrorOr<Json::Value> AudioConstraints::ToJson() const {
  if (max_sample_rate <= 0 || max_channels <= 0 || min_bit_rate <= 0 ||
      max_bit_rate < min_bit_rate) {
    return CreateParameterError("AudioConstraints");
  }

  Json::Value root;
  root["maxSampleRate"] = max_sample_rate;
  root["maxChannels"] = max_channels;
  root["minBitRate"] = min_bit_rate;
  root["maxBitRate"] = max_bit_rate;
  root["maxDelay"] = Json::Value::Int64(max_delay.count());
  return root;
}

ErrorOr<Json::Value> Dimensions::ToJson() const {
  if (width <= 0 || height <= 0 || frame_rate_denominator <= 0 ||
      frame_rate_numerator <= 0) {
    return CreateParameterError("Dimensions");
  }

  Json::Value root;
  root["width"] = width;
  root["height"] = height;

  if (frame_rate_denominator > 1) {
    root["frameRate"] =
        absl::StrCat(frame_rate_numerator, "/", frame_rate_denominator);
  } else {
    root["frameRate"] = std::to_string(frame_rate_numerator);
  }
  return root;
}

ErrorOr<Json::Value> VideoConstraints::ToJson() const {
  if (max_pixels_per_second <= 0 || min_bit_rate <= 0 ||
      max_bit_rate < min_bit_rate || max_delay.count() <= 0) {
    return CreateParameterError("VideoConstraints");
  }

  auto error_or_min_dim = min_dimensions.ToJson();
  if (error_or_min_dim.is_error()) {
    return error_or_min_dim.error();
  }

  auto error_or_max_dim = max_dimensions.ToJson();
  if (error_or_max_dim.is_error()) {
    return error_or_max_dim.error();
  }

  Json::Value root;
  root["maxPixelsPerSecond"] = max_pixels_per_second;
  root["minDimensions"] = error_or_min_dim.value();
  root["maxDimensions"] = error_or_max_dim.value();
  root["minBitRate"] = min_bit_rate;
  root["maxBitRate"] = max_bit_rate;
  root["maxDelay"] = Json::Value::Int64(max_delay.count());
  return root;
}

ErrorOr<Json::Value> Constraints::ToJson() const {
  auto audio_or_error = audio.ToJson();
  if (audio_or_error.is_error()) {
    return audio_or_error.error();
  }

  auto video_or_error = video.ToJson();
  if (video_or_error.is_error()) {
    return video_or_error.error();
  }

  Json::Value root;
  root["audio"] = audio_or_error.value();
  root["video"] = video_or_error.value();
  return root;
}

ErrorOr<Json::Value> DisplayDescription::ToJson() const {
  if (aspect_ratio.width < 1 || aspect_ratio.height < 1) {
    return CreateParameterError("DisplayDescription");
  }

  auto dimensions_or_error = dimensions.ToJson();
  if (dimensions_or_error.is_error()) {
    return dimensions_or_error.error();
  }

  Json::Value root;
  root["dimensions"] = dimensions_or_error.value();
  root["aspectRatio"] =
      absl::StrCat(aspect_ratio.width, ":", aspect_ratio.height);
  root["scaling"] = AspectRatioConstraintToJson(aspect_ratio_constraint);
  return root;
}

ErrorOr<Json::Value> Answer::ToJson() const {
  if (udp_port <= 0 || udp_port > 65535) {
    return CreateParameterError("Answer - UDP Port number");
  }

  Json::Value root;
  if (constraints) {
    auto constraints_or_error = constraints.value().ToJson();
    if (constraints_or_error.is_error()) {
      return constraints_or_error.error();
    }
    root["constraints"] = constraints_or_error.value();
  }

  if (display) {
    auto display_or_error = display.value().ToJson();
    if (display_or_error.is_error()) {
      return display_or_error.error();
    }
    root["display"] = display_or_error.value();
  }

  root["castMode"] = cast_mode.ToString();
  root["udpPort"] = udp_port;
  root["sendIndexes"] = PrimitiveVectorToJson(send_indexes);
  root["ssrcs"] = PrimitiveVectorToJson(ssrcs);
  root["receiverRtcpEventLog"] = PrimitiveVectorToJson(receiver_rtcp_event_log);
  root["receiverRtcpDscp"] = PrimitiveVectorToJson(receiver_rtcp_dscp);
  root["receiverGetStatus"] = supports_wifi_status_reporting;
  root["rtpExtensions"] = PrimitiveVectorToJson(rtp_extensions);
  return root;
}

Json::Value Answer::ToAnswerMessage() const {
  auto json_or_error = ToJson();
  if (json_or_error.is_error()) {
    return CreateInvalidAnswer(json_or_error.error());
  }

  Json::Value message_root;
  message_root[kMessageKeyType] = kMessageTypeAnswer;
  message_root[kAnswerMessageBody] = std::move(json_or_error.value());
  message_root[kResult] = kResultOk;
  return message_root;
}

Json::Value CreateInvalidAnswer(Error error) {
  Json::Value message_root;
  message_root[kMessageKeyType] = kMessageTypeAnswer;
  message_root[kResult] = kResultError;
  message_root[kErrorMessageBody][kErrorCode] = static_cast<int>(error.code());
  message_root[kErrorMessageBody][kErrorDescription] = error.message();

  return message_root;
}

}  // namespace cast
}  // namespace openscreen