| 6 | #include "rtc_base/logging.h" |
| 7 |
| 8 | #include <sstream> |
| 9 |
| 10 | namespace tgcalls { |
| 11 | namespace signaling { |
| 12 |
| 13 | static std::string uint32ToString(uint32_t value) { |
| 14 | std::ostringstream stringStream; |
| 15 | stringStream << value; |
| 16 | return stringStream.str(); |
| 17 | } |
| 18 |
| 19 | static uint32_t stringToUInt32(std::string const &string) { |
| 20 | std::stringstream stringStream(string); |
| 21 | uint32_t value = 0; |
| 22 | stringStream >> value; |
| 23 | return value; |
| 24 | } |
| 25 |
| 26 | json11::Json::object SsrcGroup_serialize(SsrcGroup const &ssrcGroup) { |
| 27 | json11::Json::object object; |
| 28 |
| 29 | json11::Json::array ssrcs; |
| 30 | for (auto ssrc : ssrcGroup.ssrcs) { |
| 31 | ssrcs.push_back(json11::Json(uint32ToString(ssrc))); |
| 32 | } |
| 33 | object.insert(std::make_pair("semantics", json11::Json(ssrcGroup.semantics))); |
| 34 | object.insert(std::make_pair("ssrcs", json11::Json(std::move(ssrcs)))); |
| 35 |
| 36 | return object; |
| 37 | } |
| 38 |
| 39 | absl::optional<SsrcGroup> SsrcGroup_parse(json11::Json::object const &object) { |
| 40 | SsrcGroup result; |
| 41 |
| 42 | const auto semantics = object.find("semantics"); |
| 43 | if (semantics == object.end() || !semantics->second.is_string()) { |
| 44 | RTC_LOG(LS_ERROR) << "Signaling: semantics must be a string"; |
| 45 | return absl::nullopt; |
| 46 | } |
| 47 | result.semantics = semantics->second.string_value(); |
| 48 |
| 49 | const auto ssrcs = object.find("ssrcs"); |
| 50 | if (ssrcs == object.end() || !ssrcs->second.is_array()) { |
| 51 | RTC_LOG(LS_ERROR) << "Signaling: ssrcs must be an array"; |
| 52 | return absl::nullopt; |
| 53 | } |
| 54 | for (const auto &ssrc : ssrcs->second.array_items()) { |
| 55 | if (ssrc.is_string()) { |
| 56 | uint32_t parsedSsrc = stringToUInt32(ssrc.string_value()); |
| 57 | if (parsedSsrc == 0) { |
| 58 | RTC_LOG(LS_ERROR) << "Signaling: parsedSsrc must not be 0"; |
| 59 | return absl::nullopt; |
| 60 | } |
| 61 | result.ssrcs.push_back(parsedSsrc); |
| 62 | } else if (ssrc.is_number()) { |
| 63 | uint32_t parsedSsrc = (uint32_t)ssrc.number_value(); |
| 64 | result.ssrcs.push_back(parsedSsrc); |
| 65 | } else { |
| 66 | RTC_LOG(LS_ERROR) << "Signaling: ssrcs item must be a string or a number"; |
| 67 | return absl::nullopt; |
| 68 | } |
| 69 | } |
| 70 |
| 71 | return result; |
| 72 | } |
| 73 |
| 74 | json11::Json::object FeedbackType_serialize(FeedbackType const &feedbackType) { |
| 75 | json11::Json::object object; |
| 76 |
| 77 | object.insert(std::make_pair("type", json11::Json(feedbackType.type))); |
| 78 | object.insert(std::make_pair("subtype", json11::Json(feedbackType.subtype))); |
| 79 |
| 80 | return object; |
| 81 | } |
| 82 |
| 83 | absl::optional<FeedbackType> FeedbackType_parse(json11::Json::object const &object) { |
| 84 | FeedbackType result; |
| 85 |
| 86 | const auto type = object.find("type"); |
| 87 | if (type == object.end() || !type->second.is_string()) { |
| 88 | RTC_LOG(LS_ERROR) << "Signaling: type must be a string"; |
| 89 | return absl::nullopt; |
| 90 | } |
| 91 | result.type = type->second.string_value(); |
| 92 |
| 93 | const auto subtype = object.find("subtype"); |
| 94 | if (subtype == object.end() || !subtype->second.is_string()) { |
| 95 | RTC_LOG(LS_ERROR) << "Signaling: subtype must be a string"; |
| 96 | return absl::nullopt; |
| 97 | } |
| 98 | result.subtype = subtype->second.string_value(); |
| 99 |
| 100 | return result; |
| 101 | } |
| 102 |
| 103 | json11::Json::object RtpExtension_serialize(webrtc::RtpExtension const &rtpExtension) { |
| 104 | json11::Json::object object; |
| 105 |
| 106 | object.insert(std::make_pair("id", json11::Json(rtpExtension.id))); |
| 107 | object.insert(std::make_pair("uri", json11::Json(rtpExtension.uri))); |
| 108 |
| 109 | return object; |
| 110 | } |
| 111 |
| 112 | absl::optional<webrtc::RtpExtension> RtpExtension_parse(json11::Json::object const &object) { |
| 113 | const auto id = object.find("id"); |
| 114 | if (id == object.end() || !id->second.is_number()) { |
| 115 | RTC_LOG(LS_ERROR) << "Signaling: id must be a number"; |
| 116 | return absl::nullopt; |
| 117 | } |
| 118 |
| 119 | const auto uri = object.find("uri"); |
| 120 | if (uri == object.end() || !uri->second.is_string()) { |
| 121 | RTC_LOG(LS_ERROR) << "Signaling: uri must be a string"; |
| 122 | return absl::nullopt; |
| 123 | } |
| 124 |
| 125 | return webrtc::RtpExtension(uri->second.string_value(), id->second.int_value()); |
| 126 | } |
| 127 |
| 128 | json11::Json::object PayloadType_serialize(PayloadType const &payloadType) { |
| 129 | json11::Json::object object; |
| 130 |
| 131 | object.insert(std::make_pair("id", json11::Json((int)payloadType.id))); |
| 132 | object.insert(std::make_pair("name", json11::Json(payloadType.name))); |
| 133 | object.insert(std::make_pair("clockrate", json11::Json((int)payloadType.clockrate))); |
| 134 | object.insert(std::make_pair("channels", json11::Json((int)payloadType.channels))); |
| 135 |
| 136 | json11::Json::array feedbackTypes; |
| 137 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 138 | feedbackTypes.push_back(FeedbackType_serialize(feedbackType)); |
| 139 | } |
| 140 | object.insert(std::make_pair("feedbackTypes", json11::Json(std::move(feedbackTypes)))); |
| 141 |
| 142 | json11::Json::object parameters; |
| 143 | for (auto it : payloadType.parameters) { |
| 144 | parameters.insert(std::make_pair(it.first, json11::Json(it.second))); |
| 145 | } |
| 146 | object.insert(std::make_pair("parameters", json11::Json(std::move(parameters)))); |
| 147 |
| 148 | return object; |
| 149 | } |
| 150 |
| 151 | absl::optional<PayloadType> PayloadType_parse(json11::Json::object const &object) { |
| 152 | PayloadType result; |
| 153 |
| 154 | const auto id = object.find("id"); |
| 155 | if (id == object.end() || !id->second.is_number()) { |
| 156 | RTC_LOG(LS_ERROR) << "Signaling: id must be a number"; |
| 157 | return absl::nullopt; |
| 158 | } |
| 159 | result.id = id->second.int_value(); |
| 160 |
| 161 | const auto name = object.find("name"); |
| 162 | if (name == object.end() || !name->second.is_string()) { |
| 163 | RTC_LOG(LS_ERROR) << "Signaling: name must be a string"; |
| 164 | return absl::nullopt; |
| 165 | } |
| 166 | result.name = name->second.string_value(); |
| 167 |
| 168 | const auto clockrate = object.find("clockrate"); |
| 169 | if (clockrate == object.end() || !clockrate->second.is_number()) { |
| 170 | RTC_LOG(LS_ERROR) << "Signaling: clockrate must be a number"; |
| 171 | return absl::nullopt; |
| 172 | } |
| 173 | result.clockrate = clockrate->second.int_value(); |
| 174 |
| 175 | const auto channels = object.find("channels"); |
| 176 | if (channels != object.end()) { |
| 177 | if (!channels->second.is_number()) { |
| 178 | RTC_LOG(LS_ERROR) << "Signaling: channels must be a number"; |
| 179 | return absl::nullopt; |
| 180 | } |
| 181 | result.channels = channels->second.int_value(); |
| 182 | } |
| 183 |
| 184 | const auto feedbackTypes = object.find("feedbackTypes"); |
| 185 | if (feedbackTypes != object.end()) { |
| 186 | if (!feedbackTypes->second.is_array()) { |
| 187 | RTC_LOG(LS_ERROR) << "Signaling: feedbackTypes must be an array"; |
| 188 | return absl::nullopt; |
| 189 | } |
| 190 | for (const auto &feedbackType : feedbackTypes->second.array_items()) { |
| 191 | if (!feedbackType.is_object()) { |
| 192 | RTC_LOG(LS_ERROR) << "Signaling: feedbackTypes items must be objects"; |
| 193 | return absl::nullopt; |
| 194 | } |
| 195 | if (const auto parsedFeedbackType = FeedbackType_parse(feedbackType.object_items())) { |
| 196 | result.feedbackTypes.push_back(parsedFeedbackType.value()); |
| 197 | } else { |
| 198 | RTC_LOG(LS_ERROR) << "Signaling: could not parse FeedbackType"; |
| 199 | return absl::nullopt; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 |
| 204 | const auto parameters = object.find("parameters"); |
| 205 | if (parameters != object.end()) { |
| 206 | if (!parameters->second.is_object()) { |
| 207 | RTC_LOG(LS_ERROR) << "Signaling: parameters must be an object"; |
| 208 | return absl::nullopt; |
| 209 | } |
| 210 | for (const auto &item : parameters->second.object_items()) { |
| 211 | if (!item.second.is_string()) { |
| 212 | RTC_LOG(LS_ERROR) << "Signaling: parameters items must be strings"; |
| 213 | return absl::nullopt; |
| 214 | } |
| 215 | result.parameters.push_back(std::make_pair(item.first, item.second.string_value())); |
| 216 | } |
| 217 | } |
| 218 |
| 219 | return result; |
| 220 | } |
| 221 |
| 222 | json11::Json::object MediaContent_serialize(MediaContent const &mediaContent) { |
| 223 | json11::Json::object object; |
| 224 |
| 225 | std::string mappedType; |
| 226 | switch (mediaContent.type) { |
| 227 | case MediaContent::Type::Audio: { |
| 228 | mappedType = "audio"; |
| 229 | break; |
| 230 | } |
| 231 | case MediaContent::Type::Video: { |
| 232 | mappedType = "video"; |
| 233 | break; |
| 234 | } |
| 235 | default: { |
| 236 | RTC_FATAL() << "Unknown media type"; |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | object.insert(std::make_pair("type", mappedType)); |
| 241 |
| 242 | object.insert(std::make_pair("ssrc", json11::Json(uint32ToString(mediaContent.ssrc)))); |
| 243 |
| 244 | if (mediaContent.ssrcGroups.size() != 0) { |
| 245 | json11::Json::array ssrcGroups; |
| 246 | for (const auto &group : mediaContent.ssrcGroups) { |
| 247 | ssrcGroups.push_back(SsrcGroup_serialize(group)); |
| 248 | } |
| 249 | object.insert(std::make_pair("ssrcGroups", json11::Json(std::move(ssrcGroups)))); |
| 250 | } |
| 251 |
| 252 | if (mediaContent.payloadTypes.size() != 0) { |
| 253 | json11::Json::array payloadTypes; |
| 254 | for (const auto &payloadType : mediaContent.payloadTypes) { |
| 255 | payloadTypes.push_back(PayloadType_serialize(payloadType)); |
| 256 | } |
| 257 | object.insert(std::make_pair("payloadTypes", json11::Json(std::move(payloadTypes)))); |
| 258 | } |
| 259 |
| 260 | json11::Json::array rtpExtensions; |
| 261 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 262 | rtpExtensions.push_back(RtpExtension_serialize(rtpExtension)); |
| 263 | } |
| 264 | object.insert(std::make_pair("rtpExtensions", json11::Json(std::move(rtpExtensions)))); |
| 265 |
| 266 | return object; |
| 267 | } |
| 268 |
| 269 | absl::optional<MediaContent> MediaContent_parse(json11::Json::object const &object) { |
| 270 | MediaContent result; |
| 271 |
| 272 | const auto type = object.find("type"); |
| 273 | if (type == object.end() || !type->second.is_string()) { |
| 274 | RTC_LOG(LS_ERROR) << "Signaling: type must be a string"; |
| 275 | return absl::nullopt; |
| 276 | } |
| 277 | if (type->second.string_value() == "audio") { |
| 278 | result.type = MediaContent::Type::Audio; |
| 279 | } else if (type->second.string_value() == "video") { |
| 280 | result.type = MediaContent::Type::Video; |
| 281 | } else { |
| 282 | RTC_LOG(LS_ERROR) << "Signaling: type must be one of [\"audio\", \"video\"]"; |
| 283 | return absl::nullopt; |
| 284 | } |
| 285 |
| 286 | const auto ssrc = object.find("ssrc"); |
| 287 | if (ssrc == object.end()) { |
| 288 | RTC_LOG(LS_ERROR) << "Signaling: ssrc must be present"; |
| 289 | return absl::nullopt; |
| 290 | } |
| 291 | if (ssrc->second.is_string()) { |
| 292 | result.ssrc = stringToUInt32(ssrc->second.string_value()); |
| 293 | } else if (ssrc->second.is_number()) { |
| 294 | result.ssrc = (uint32_t)ssrc->second.number_value(); |
| 295 | } else { |
| 296 | RTC_LOG(LS_ERROR) << "Signaling: ssrc must be a string or a number"; |
| 297 | return absl::nullopt; |
| 298 | } |
| 299 |
| 300 | const auto ssrcGroups = object.find("ssrcGroups"); |
| 301 | if (ssrcGroups != object.end()) { |
| 302 | if (!ssrcGroups->second.is_array()) { |
| 303 | RTC_LOG(LS_ERROR) << "Signaling: ssrcGroups must be an array"; |
| 304 | return absl::nullopt; |
| 305 | } |
| 306 | for (const auto &ssrcGroup : ssrcGroups->second.array_items()) { |
| 307 | if (!ssrcGroup.is_object()) { |
| 308 | RTC_LOG(LS_ERROR) << "Signaling: ssrcsGroups items must be objects"; |
| 309 | return absl::nullopt; |
| 310 | } |
| 311 | if (const auto parsedSsrcGroup = SsrcGroup_parse(ssrcGroup.object_items())) { |
| 312 | result.ssrcGroups.push_back(parsedSsrcGroup.value()); |
| 313 | } else { |
| 314 | RTC_LOG(LS_ERROR) << "Signaling: could not parse SsrcGroup"; |
| 315 | return absl::nullopt; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 |
| 320 | const auto payloadTypes = object.find("payloadTypes"); |
| 321 | if (payloadTypes != object.end()) { |
| 322 | if (!payloadTypes->second.is_array()) { |
| 323 | RTC_LOG(LS_ERROR) << "Signaling: payloadTypes must be an array"; |
| 324 | return absl::nullopt; |
| 325 | } |
| 326 | for (const auto &payloadType : payloadTypes->second.array_items()) { |
| 327 | if (!payloadType.is_object()) { |
| 328 | RTC_LOG(LS_ERROR) << "Signaling: payloadTypes items must be objects"; |
| 329 | return absl::nullopt; |
| 330 | } |
| 331 | if (const auto parsedPayloadType = PayloadType_parse(payloadType.object_items())) { |
| 332 | result.payloadTypes.push_back(parsedPayloadType.value()); |
| 333 | } else { |
| 334 | RTC_LOG(LS_ERROR) << "Signaling: could not parse PayloadType"; |
| 335 | return absl::nullopt; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 |
| 340 | const auto rtpExtensions = object.find("rtpExtensions"); |
| 341 | if (rtpExtensions != object.end()) { |
| 342 | if (!rtpExtensions->second.is_array()) { |
| 343 | RTC_LOG(LS_ERROR) << "Signaling: rtpExtensions must be an array"; |
| 344 | return absl::nullopt; |
| 345 | } |
| 346 | for (const auto &rtpExtension : rtpExtensions->second.array_items()) { |
| 347 | if (!rtpExtension.is_object()) { |
| 348 | RTC_LOG(LS_ERROR) << "Signaling: rtpExtensions items must be objects"; |
| 349 | return absl::nullopt; |
| 350 | } |
| 351 | if (const auto parsedRtpExtension = RtpExtension_parse(rtpExtension.object_items())) { |
| 352 | result.rtpExtensions.push_back(parsedRtpExtension.value()); |
| 353 | } else { |
| 354 | RTC_LOG(LS_ERROR) << "Signaling: could not parse RtpExtension"; |
| 355 | return absl::nullopt; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 |
| 360 | return result; |
| 361 | } |
| 362 |
| 363 | std::vector<uint8_t> InitialSetupMessage_serialize(const InitialSetupMessage * const message) { |
| 364 | json11::Json::object object; |
| 365 |
| 366 | object.insert(std::make_pair("@type", json11::Json("InitialSetup"))); |
| 367 | object.insert(std::make_pair("ufrag", json11::Json(message->ufrag))); |
| 368 | object.insert(std::make_pair("pwd", json11::Json(message->pwd))); |
| 369 | object.insert(std::make_pair("renomination", json11::Json(message->supportsRenomination))); |
| 370 |
| 371 | json11::Json::array jsonFingerprints; |
| 372 | for (const auto &fingerprint : message->fingerprints) { |
| 373 | json11::Json::object jsonFingerprint; |
| 374 | jsonFingerprint.insert(std::make_pair("hash", json11::Json(fingerprint.hash))); |
| 375 | jsonFingerprint.insert(std::make_pair("setup", json11::Json(fingerprint.setup))); |
| 376 | jsonFingerprint.insert(std::make_pair("fingerprint", json11::Json(fingerprint.fingerprint))); |
| 377 | jsonFingerprints.emplace_back(std::move(jsonFingerprint)); |
| 378 | } |
| 379 | object.insert(std::make_pair("fingerprints", json11::Json(std::move(jsonFingerprints)))); |
| 380 |
| 381 | auto json = json11::Json(std::move(object)); |
| 382 | std::string result = json.dump(); |
| 383 | return std::vector<uint8_t>(result.begin(), result.end()); |
| 384 | } |
| 385 |
| 386 | absl::optional<InitialSetupMessage> InitialSetupMessage_parse(json11::Json::object const &object) { |
| 387 | const auto ufrag = object.find("ufrag"); |
| 388 | if (ufrag == object.end() || !ufrag->second.is_string()) { |
| 389 | RTC_LOG(LS_ERROR) << "Signaling: ufrag must be a string"; |
| 390 | return absl::nullopt; |
| 391 | } |
| 392 | const auto pwd = object.find("pwd"); |
| 393 | if (pwd == object.end() || !pwd->second.is_string()) { |
| 394 | RTC_LOG(LS_ERROR) << "Signaling: pwd must be a string"; |
| 395 | return absl::nullopt; |
| 396 | } |
| 397 | const auto renomination = object.find("renomination"); |
| 398 | bool renominationValue = false; |
| 399 | if (renomination != object.end() && renomination->second.is_bool()) { |
| 400 | renominationValue = renomination->second.bool_value(); |
| 401 | } |
| 402 | const auto fingerprints = object.find("fingerprints"); |
| 403 | if (fingerprints == object.end() || !fingerprints->second.is_array()) { |
| 404 | RTC_LOG(LS_ERROR) << "Signaling: fingerprints must be an array"; |
| 405 | return absl::nullopt; |
| 406 | } |
| 407 | std::vector<DtlsFingerprint> parsedFingerprints; |
| 408 | for (const auto &fingerprintObject : fingerprints->second.array_items()) { |
| 409 | if (!fingerprintObject.is_object()) { |
| 410 | RTC_LOG(LS_ERROR) << "Signaling: fingerprints items must be objects"; |
| 411 | return absl::nullopt; |
| 412 | } |
| 413 | const auto hash = fingerprintObject.object_items().find("hash"); |
| 414 | if (hash == fingerprintObject.object_items().end() || !hash->second.is_string()) { |
| 415 | RTC_LOG(LS_ERROR) << "Signaling: hash must be a string"; |
| 416 | return absl::nullopt; |
| 417 | } |
| 418 | const auto setup = fingerprintObject.object_items().find("setup"); |
| 419 | if (setup == fingerprintObject.object_items().end() || !setup->second.is_string()) { |
| 420 | RTC_LOG(LS_ERROR) << "Signaling: setup must be a string"; |
| 421 | return absl::nullopt; |
| 422 | } |
| 423 | const auto fingerprint = fingerprintObject.object_items().find("fingerprint"); |
| 424 | if (fingerprint == fingerprintObject.object_items().end() || !fingerprint->second.is_string()) { |
| 425 | RTC_LOG(LS_ERROR) << "Signaling: fingerprint must be a string"; |
| 426 | return absl::nullopt; |
| 427 | } |
| 428 |
| 429 | DtlsFingerprint parsedFingerprint; |
| 430 | parsedFingerprint.hash = hash->second.string_value(); |
| 431 | parsedFingerprint.setup = setup->second.string_value(); |
| 432 | parsedFingerprint.fingerprint = fingerprint->second.string_value(); |
| 433 |
| 434 | parsedFingerprints.push_back(std::move(parsedFingerprint)); |
| 435 | } |
| 436 |
| 437 | InitialSetupMessage message; |
| 438 | message.ufrag = ufrag->second.string_value(); |
| 439 | message.pwd = pwd->second.string_value(); |
| 440 | message.supportsRenomination = renominationValue; |
| 441 | message.fingerprints = std::move(parsedFingerprints); |
| 442 |
| 443 | return message; |
| 444 | } |
| 445 |
| 446 | std::vector<uint8_t> NegotiateChannelsMessage_serialize(const NegotiateChannelsMessage * const message) { |
| 447 | json11::Json::object object; |
| 448 |
| 449 | object.insert(std::make_pair("@type", json11::Json("NegotiateChannels"))); |
| 450 |
| 451 | object.insert(std::make_pair("exchangeId", json11::Json(uint32ToString(message->exchangeId)))); |
| 452 |
| 453 | json11::Json::array contents; |
| 454 | for (const auto &content : message->contents) { |
| 455 | contents.push_back(json11::Json(MediaContent_serialize(content))); |
| 456 | } |
| 457 | object.insert(std::make_pair("contents", std::move(contents))); |
| 458 |
| 459 | auto json = json11::Json(std::move(object)); |
| 460 | std::string result = json.dump(); |
| 461 | return std::vector<uint8_t>(result.begin(), result.end()); |
| 462 | } |
| 463 |
| 464 | absl::optional<NegotiateChannelsMessage> NegotiateChannelsMessage_parse(json11::Json::object const &object) { |
| 465 | NegotiateChannelsMessage message; |
| 466 |
| 467 | const auto exchangeId = object.find("exchangeId"); |
| 468 |
| 469 | if (exchangeId == object.end()) { |
| 470 | RTC_LOG(LS_ERROR) << "Signaling: exchangeId must be present"; |
| 471 | return absl::nullopt; |
| 472 | } else if (exchangeId->second.is_string()) { |
| 473 | message.exchangeId = stringToUInt32(exchangeId->second.string_value()); |
| 474 | } else if (exchangeId->second.is_number()) { |
| 475 | message.exchangeId = (uint32_t)exchangeId->second.number_value(); |
| 476 | } else { |
| 477 | RTC_LOG(LS_ERROR) << "Signaling: exchangeId must be a string or a number"; |
| 478 | return absl::nullopt; |
| 479 | } |
| 480 |
| 481 | const auto contents = object.find("contents"); |
| 482 | if (contents != object.end()) { |
| 483 | if (!contents->second.is_array()) { |
| 484 | RTC_LOG(LS_ERROR) << "Signaling: contents must be an array"; |
| 485 | return absl::nullopt; |
| 486 | } |
| 487 | for (const auto &content : contents->second.array_items()) { |
| 488 | if (!content.is_object()) { |
| 489 | RTC_LOG(LS_ERROR) << "Signaling: contents items must be objects"; |
| 490 | return absl::nullopt; |
| 491 | } |
| 492 | if (auto parsedContent = MediaContent_parse(content.object_items())) { |
| 493 | message.contents.push_back(std::move(parsedContent.value())); |
| 494 | } else { |
| 495 | RTC_LOG(LS_ERROR) << "Signaling: could not parse MediaContent"; |
| 496 | return absl::nullopt; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 |
| 501 | return message; |
| 502 | } |
| 503 |
| 504 | json11::Json::object ConnectionAddress_serialize(ConnectionAddress const &connectionAddress) { |
| 505 | json11::Json::object object; |
| 506 |
| 507 | object.insert(std::make_pair("ip", json11::Json(connectionAddress.ip))); |
| 508 | object.insert(std::make_pair("port", json11::Json(connectionAddress.port))); |
| 509 |
| 510 | return object; |
| 511 | } |
| 512 |
| 513 | absl::optional<ConnectionAddress> ConnectionAddress_parse(json11::Json::object const &object) { |
| 514 | const auto ip = object.find("ip"); |
| 515 | if (ip == object.end() || !ip->second.is_string()) { |
| 516 | RTC_LOG(LS_ERROR) << "Signaling: ip must be a string"; |
| 517 | return absl::nullopt; |
| 518 | } |
| 519 |
| 520 | const auto port = object.find("port"); |
| 521 | if (port == object.end() || !port->second.is_number()) { |
| 522 | RTC_LOG(LS_ERROR) << "Signaling: port must be a number"; |
| 523 | return absl::nullopt; |
| 524 | } |
| 525 |
| 526 | ConnectionAddress address; |
| 527 | address.ip = ip->second.string_value(); |
| 528 | address.port = port->second.int_value(); |
| 529 | return address; |
| 530 | } |
| 531 |
| 532 | std::vector<uint8_t> CandidatesMessage_serialize(const CandidatesMessage * const message) { |
| 533 | json11::Json::array candidates; |
| 534 | for (const auto &candidate : message->iceCandidates) { |
| 535 | json11::Json::object candidateObject; |
| 536 |
| 537 | candidateObject.insert(std::make_pair("sdpString", json11::Json(candidate.sdpString))); |
| 538 |
| 539 | candidates.emplace_back(std::move(candidateObject)); |
| 540 | } |
| 541 |
| 542 | json11::Json::object object; |
| 543 |
| 544 | object.insert(std::make_pair("@type", json11::Json("Candidates"))); |
| 545 | object.insert(std::make_pair("candidates", json11::Json(std::move(candidates)))); |
| 546 |
| 547 | auto json = json11::Json(std::move(object)); |
| 548 | std::string result = json.dump(); |
| 549 | return std::vector<uint8_t>(result.begin(), result.end()); |
| 550 | } |
| 551 |
| 552 | absl::optional<CandidatesMessage> CandidatesMessage_parse(json11::Json::object const &object) { |
| 553 | const auto candidates = object.find("candidates"); |
| 554 | if (candidates == object.end() || !candidates->second.is_array()) { |
| 555 | RTC_LOG(LS_ERROR) << "Signaling: candidates must be an array"; |
| 556 | return absl::nullopt; |
| 557 | } |
| 558 |
| 559 | std::vector<IceCandidate> parsedCandidates; |
| 560 | for (const auto &candidateObject : candidates->second.array_items()) { |
| 561 | if (!candidateObject.is_object()) { |
| 562 | RTC_LOG(LS_ERROR) << "Signaling: candidates items must be objects"; |
| 563 | return absl::nullopt; |
| 564 | } |
| 565 |
| 566 | IceCandidate candidate; |
| 567 |
| 568 | const auto sdpString = candidateObject.object_items().find("sdpString"); |
| 569 | if (sdpString == candidateObject.object_items().end() || !sdpString->second.is_string()) { |
| 570 | RTC_LOG(LS_ERROR) << "Signaling: sdpString must be a string"; |
| 571 | return absl::nullopt; |
| 572 | } |
| 573 | candidate.sdpString = sdpString->second.string_value(); |
| 574 |
| 575 | parsedCandidates.push_back(std::move(candidate)); |
| 576 | } |
| 577 |
| 578 | CandidatesMessage message; |
| 579 | message.iceCandidates = std::move(parsedCandidates); |
| 580 |
| 581 | return message; |
| 582 | } |
| 583 |
| 584 | std::vector<uint8_t> MediaStateMessage_serialize(const MediaStateMessage * const message) { |
| 585 | json11::Json::object object; |
| 586 |
| 587 | object.insert(std::make_pair("@type", json11::Json("MediaState"))); |
| 588 | object.insert(std::make_pair("muted", json11::Json(message->isMuted))); |
| 589 | object.insert(std::make_pair("lowBattery", json11::Json(message->isBatteryLow))); |
| 590 |
| 591 | std::string videoStateValue; |
| 592 | switch (message->videoState) { |
| 593 | case MediaStateMessage::VideoState::Inactive: { |
| 594 | videoStateValue = "inactive"; |
| 595 | break; |
| 596 | } |
| 597 | case MediaStateMessage::VideoState::Suspended: { |
| 598 | videoStateValue = "suspended"; |
| 599 | break; |
| 600 | } |
| 601 | case MediaStateMessage::VideoState::Active: { |
| 602 | videoStateValue = "active"; |
| 603 | break; |
| 604 | } |
| 605 | default: { |
| 606 | RTC_FATAL() << "Unknown videoState"; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | object.insert(std::make_pair("videoState", json11::Json(videoStateValue))); |
| 611 |
| 612 | int videoRotationValue = 0; |
| 613 | switch (message->videoRotation) { |
| 614 | case MediaStateMessage::VideoRotation::Rotation0: { |
| 615 | videoRotationValue = 0; |
| 616 | break; |
| 617 | } |
| 618 | case MediaStateMessage::VideoRotation::Rotation90: { |
| 619 | videoRotationValue = 90; |
| 620 | break; |
| 621 | } |
| 622 | case MediaStateMessage::VideoRotation::Rotation180: { |
| 623 | videoRotationValue = 180; |
| 624 | break; |
| 625 | } |
| 626 | case MediaStateMessage::VideoRotation::Rotation270: { |
| 627 | videoRotationValue = 270; |
| 628 | break; |
| 629 | } |
| 630 | default: { |
| 631 | RTC_FATAL() << "Unknown videoRotation"; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | object.insert(std::make_pair("videoRotation", json11::Json(videoRotationValue))); |
| 636 |
| 637 | std::string screencastStateValue; |
| 638 | switch (message->screencastState) { |
| 639 | case MediaStateMessage::VideoState::Inactive: { |
| 640 | screencastStateValue = "inactive"; |
| 641 | break; |
| 642 | } |
| 643 | case MediaStateMessage::VideoState::Suspended: { |
| 644 | screencastStateValue = "suspended"; |
| 645 | break; |
| 646 | } |
| 647 | case MediaStateMessage::VideoState::Active: { |
| 648 | screencastStateValue = "active"; |
| 649 | break; |
| 650 | } |
| 651 | default: { |
| 652 | RTC_FATAL() << "Unknown videoState"; |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | object.insert(std::make_pair("screencastState", json11::Json(screencastStateValue))); |
| 657 |
| 658 | auto json = json11::Json(std::move(object)); |
| 659 | std::string result = json.dump(); |
| 660 | return std::vector<uint8_t>(result.begin(), result.end()); |
| 661 | } |
| 662 |
| 663 | absl::optional<MediaStateMessage> MediaStateMessage_parse(json11::Json::object const &object) { |
| 664 | MediaStateMessage message; |
| 665 |
| 666 | const auto muted = object.find("muted"); |
| 667 | if (muted != object.end()) { |
| 668 | if (!muted->second.is_bool()) { |
| 669 | RTC_LOG(LS_ERROR) << "Signaling: muted must be a bool"; |
| 670 | return absl::nullopt; |
| 671 | } |
| 672 | message.isMuted = muted->second.bool_value(); |
| 673 | } |
| 674 |
| 675 | const auto lowBattery = object.find("lowBattery"); |
| 676 | if (lowBattery != object.end()) { |
| 677 | if (!lowBattery->second.is_bool()) { |
| 678 | RTC_LOG(LS_ERROR) << "Signaling: lowBattery must be a bool"; |
| 679 | return absl::nullopt; |
| 680 | } |
| 681 | message.isBatteryLow = lowBattery->second.bool_value(); |
| 682 | } |
| 683 |
| 684 | const auto videoState = object.find("videoState"); |
| 685 | if (videoState != object.end()) { |
| 686 | if (!videoState->second.is_string()) { |
| 687 | RTC_LOG(LS_ERROR) << "Signaling: videoState must be a string"; |
| 688 | return absl::nullopt; |
| 689 | } |
| 690 | if (videoState->second.string_value() == "inactive") { |
| 691 | message.videoState = MediaStateMessage::VideoState::Inactive; |
| 692 | } else if (videoState->second.string_value() == "suspended") { |
| 693 | message.videoState = MediaStateMessage::VideoState::Suspended; |
| 694 | } else if (videoState->second.string_value() == "active") { |
| 695 | message.videoState = MediaStateMessage::VideoState::Active; |
| 696 | } else { |
| 697 | RTC_LOG(LS_ERROR) << "videoState must be one of [\"inactive\", \"suspended\", \"active\"]"; |
| 698 | } |
| 699 | } else { |
| 700 | message.videoState = MediaStateMessage::VideoState::Inactive; |
| 701 | } |
| 702 |
| 703 | const auto screencastState = object.find("screencastState"); |
| 704 | if (screencastState != object.end()) { |
| 705 | if (!screencastState->second.is_string()) { |
| 706 | RTC_LOG(LS_ERROR) << "Signaling: screencastState must be a string"; |
| 707 | return absl::nullopt; |
| 708 | } |
| 709 | if (screencastState->second.string_value() == "inactive") { |
| 710 | message.screencastState = MediaStateMessage::VideoState::Inactive; |
| 711 | } else if (screencastState->second.string_value() == "suspended") { |
| 712 | message.screencastState = MediaStateMessage::VideoState::Suspended; |
| 713 | } else if (screencastState->second.string_value() == "active") { |
| 714 | message.screencastState = MediaStateMessage::VideoState::Active; |
| 715 | } else { |
| 716 | RTC_LOG(LS_ERROR) << "Signaling: screencastState must be one of [\"inactive\", \"suspended\", \"active\"]"; |
| 717 | } |
| 718 | } else { |
| 719 | message.screencastState = MediaStateMessage::VideoState::Inactive; |
| 720 | } |
| 721 |
| 722 | const auto videoRotation = object.find("videoRotation"); |
| 723 | if (videoRotation != object.end()) { |
| 724 | if (!videoRotation->second.is_number()) { |
| 725 | RTC_LOG(LS_ERROR) << "Signaling: videoRotation must be a number"; |
| 726 | return absl::nullopt; |
| 727 | } |
| 728 | if (videoState->second.int_value() == 0) { |
| 729 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation0; |
| 730 | } else if (videoState->second.int_value() == 90) { |
| 731 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation90; |
| 732 | } else if (videoState->second.int_value() == 180) { |
| 733 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation180; |
| 734 | } else if (videoState->second.int_value() == 270) { |
| 735 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation270; |
| 736 | } else { |
| 737 | RTC_LOG(LS_ERROR) << "Signaling: videoRotation must be one of [0, 90, 180, 270]"; |
| 738 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation0; |
| 739 | } |
| 740 | } else { |
| 741 | message.videoRotation = MediaStateMessage::VideoRotation::Rotation0; |
| 742 | } |
| 743 |
| 744 | return message; |
| 745 | } |
| 746 |
| 747 | std::vector<uint8_t> Message::serialize() const { |
| 748 | if (const auto initialSetup = absl::get_if<InitialSetupMessage>(&data)) { |
| 749 | return InitialSetupMessage_serialize(initialSetup); |
| 750 | } else if (const auto candidates = absl::get_if<CandidatesMessage>(&data)) { |
| 751 | return CandidatesMessage_serialize(candidates); |
| 752 | } else if (const auto mediaState = absl::get_if<MediaStateMessage>(&data)) { |
| 753 | return MediaStateMessage_serialize(mediaState); |
| 754 | } else if (const auto negotiateChannels = absl::get_if<NegotiateChannelsMessage>(&data)) { |
| 755 | return NegotiateChannelsMessage_serialize(negotiateChannels); |
| 756 | } else { |
| 757 | return {}; |
| 758 | } |
| 759 | } |
| 760 |
| 761 | absl::optional<Message> Message::parse(const std::vector<uint8_t> &data) { |
| 762 | std::string parsingError; |
| 763 | auto json = json11::Json::parse(std::string(data.begin(), data.end()), parsingError); |
| 764 | if (json.type() != json11::Json::OBJECT) { |
| 765 | RTC_LOG(LS_ERROR) << "Signaling: message must be an object"; |
| 766 | return absl::nullopt; |
| 767 | } |
| 768 |
| 769 | auto type = json.object_items().find("@type"); |
| 770 | if (type == json.object_items().end()) { |
| 771 | RTC_LOG(LS_ERROR) << "Signaling: message does not contain @type attribute"; |
| 772 | return absl::nullopt; |
| 773 | } |
| 774 | if (!type->second.is_string()) { |
| 775 | RTC_LOG(LS_ERROR) << "Signaling: @type attribute must be a string"; |
| 776 | return absl::nullopt; |
| 777 | } |
| 778 | if (type->second.string_value() == "InitialSetup") { |
| 779 | auto parsed = InitialSetupMessage_parse(json.object_items()); |
| 780 | if (!parsed) { |
| 781 | RTC_LOG(LS_ERROR) << "Signaling: could not parse " << type->second.string_value() << " message"; |
| 782 | return absl::nullopt; |
| 783 | } |
| 784 | Message message; |
| 785 | message.data = std::move(parsed.value()); |
| 786 | return message; |
| 787 | } else if (type->second.string_value() == "NegotiateChannels") { |
| 788 | auto parsed = NegotiateChannelsMessage_parse(json.object_items()); |
| 789 | if (!parsed) { |
| 790 | RTC_LOG(LS_ERROR) << "Signaling: could not parse " << type->second.string_value() << " message"; |
| 791 | return absl::nullopt; |
| 792 | } |
| 793 | Message message; |
| 794 | message.data = std::move(parsed.value()); |
| 795 | return message; |
| 796 | } else if (type->second.string_value() == "Candidates") { |
| 797 | auto parsed = CandidatesMessage_parse(json.object_items()); |
| 798 | if (!parsed) { |
| 799 | RTC_LOG(LS_ERROR) << "Signaling: could not parse " << type->second.string_value() << " message"; |
| 800 | return absl::nullopt; |
| 801 | } |
| 802 | Message message; |
| 803 | message.data = std::move(parsed.value()); |
| 804 | return message; |
| 805 | } else if (type->second.string_value() == "MediaState") { |
| 806 | auto parsed = MediaStateMessage_parse(json.object_items()); |
| 807 | if (!parsed) { |
| 808 | RTC_LOG(LS_ERROR) << "Signaling: could not parse " << type->second.string_value() << " message"; |
| 809 | return absl::nullopt; |
| 810 | } |
| 811 | Message message; |
| 812 | message.data = std::move(parsed.value()); |
| 813 | return message; |
| 814 | } else { |
| 815 | RTC_LOG(LS_ERROR) << "Signaling: unknown message type " << type->second.string_value(); |
| 816 | return absl::nullopt; |
| 817 | } |
| 818 | } |
| 819 |
| 820 | } // namespace signaling |
| 821 |
| 822 | } // namespace tgcalls |
| 823 |