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