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