| 6 | namespace tgcalls { |
| 7 |
| 8 | namespace { |
| 9 |
| 10 | absl::optional<int32_t> parseInt(json11::Json::object const &object, std::string const &key) { |
| 11 | const auto value = object.find(key); |
| 12 | if (value == object.end() || !value->second.is_number()) { |
| 13 | return absl::nullopt; |
| 14 | } |
| 15 | return value->second.int_value(); |
| 16 | } |
| 17 |
| 18 | absl::optional<std::string> parseString(json11::Json::object const &object, std::string const &key) { |
| 19 | const auto value = object.find(key); |
| 20 | if (value == object.end() || !value->second.is_string()) { |
| 21 | return absl::nullopt; |
| 22 | } |
| 23 | return value->second.string_value(); |
| 24 | } |
| 25 |
| 26 | template <typename Out> |
| 27 | void splitString(const std::string &s, char delim, Out result) { |
| 28 | std::istringstream iss(s); |
| 29 | std::string item; |
| 30 | while (std::getline(iss, item, delim)) { |
| 31 | *result++ = item; |
| 32 | } |
| 33 | } |
| 34 |
| 35 | std::vector<std::string> splitString(const std::string &s, char delim) { |
| 36 | std::vector<std::string> elems; |
| 37 | splitString(s, delim, std::back_inserter(elems)); |
| 38 | return elems; |
| 39 | } |
| 40 |
| 41 | absl::optional<GroupJoinTransportDescription> parseTransportDescription(json11::Json::object const &object) { |
| 42 | GroupJoinTransportDescription result; |
| 43 |
| 44 | if (const auto pwd = parseString(object, "pwd")) { |
| 45 | result.pwd = pwd.value(); |
| 46 | } else { |
| 47 | return absl::nullopt; |
| 48 | } |
| 49 |
| 50 | if (const auto ufrag = parseString(object, "ufrag")) { |
| 51 | result.ufrag = ufrag.value(); |
| 52 | } else { |
| 53 | return absl::nullopt; |
| 54 | } |
| 55 |
| 56 | const auto fingerprints = object.find("fingerprints"); |
| 57 | if (fingerprints == object.end() || !fingerprints->second.is_array()) { |
| 58 | return absl::nullopt; |
| 59 | } |
| 60 | for (const auto &fingerprint : fingerprints->second.array_items()) { |
| 61 | if (!fingerprint.is_object()) { |
| 62 | return absl::nullopt; |
| 63 | } |
| 64 |
| 65 | GroupJoinTransportDescription::Fingerprint parsedFingerprint; |
| 66 |
| 67 | if (const auto hash = parseString(fingerprint.object_items(), "hash")) { |
| 68 | parsedFingerprint.hash = hash.value(); |
| 69 | } else { |
| 70 | return absl::nullopt; |
| 71 | } |
| 72 |
| 73 | if (const auto fingerprintValue = parseString(fingerprint.object_items(), "fingerprint")) { |
| 74 | parsedFingerprint.fingerprint = fingerprintValue.value(); |
| 75 | } else { |
| 76 | return absl::nullopt; |
| 77 | } |
| 78 |
| 79 | if (const auto setup = parseString(fingerprint.object_items(), "setup")) { |
| 80 | parsedFingerprint.setup = setup.value(); |
| 81 | } else { |
| 82 | return absl::nullopt; |
| 83 | } |
| 84 |
| 85 | result.fingerprints.push_back(std::move(parsedFingerprint)); |
| 86 | } |
| 87 |
| 88 | const auto candidates = object.find("candidates"); |
| 89 | if (candidates == object.end() || !candidates->second.is_array()) { |
| 90 | return absl::nullopt; |
| 91 | } |
| 92 | for (const auto &candidate : candidates->second.array_items()) { |
| 93 | if (!candidate.is_object()) { |
| 94 | return absl::nullopt; |
| 95 | } |
| 96 |
| 97 | GroupJoinTransportDescription::Candidate parsedCandidate; |
| 98 |
| 99 | if (const auto port = parseString(candidate.object_items(), "port")) { |
| 100 | parsedCandidate.port = port.value(); |
| 101 | } else { |
| 102 | return absl::nullopt; |
| 103 | } |
| 104 |
| 105 | if (const auto protocol = parseString(candidate.object_items(), "protocol")) { |
| 106 | parsedCandidate.protocol = protocol.value(); |
| 107 | } else { |
| 108 | return absl::nullopt; |
| 109 | } |
| 110 |
| 111 | if (const auto network = parseString(candidate.object_items(), "network")) { |
| 112 | parsedCandidate.network = network.value(); |
| 113 | } else { |
| 114 | return absl::nullopt; |
| 115 | } |
| 116 |
| 117 | if (const auto generation = parseString(candidate.object_items(), "generation")) { |
| 118 | parsedCandidate.generation = generation.value(); |
| 119 | } else { |
| 120 | return absl::nullopt; |
| 121 | } |
| 122 |
| 123 | if (const auto id = parseString(candidate.object_items(), "id")) { |
| 124 | parsedCandidate.id = id.value(); |
| 125 | } else { |
| 126 | return absl::nullopt; |
| 127 | } |
| 128 |
| 129 | if (const auto component = parseString(candidate.object_items(), "component")) { |
| 130 | parsedCandidate.component = component.value(); |
| 131 | } else { |
| 132 | return absl::nullopt; |
| 133 | } |
| 134 |
| 135 | if (const auto foundation = parseString(candidate.object_items(), "foundation")) { |
| 136 | parsedCandidate.foundation = foundation.value(); |
| 137 | } else { |
| 138 | return absl::nullopt; |
| 139 | } |
| 140 |
| 141 | if (const auto priority = parseString(candidate.object_items(), "priority")) { |
| 142 | parsedCandidate.priority = priority.value(); |
| 143 | } else { |
| 144 | return absl::nullopt; |
| 145 | } |
| 146 |
| 147 | if (const auto ip = parseString(candidate.object_items(), "ip")) { |
| 148 | parsedCandidate.ip = ip.value(); |
| 149 | } else { |
| 150 | return absl::nullopt; |
| 151 | } |
| 152 |
| 153 | if (const auto type = parseString(candidate.object_items(), "type")) { |
| 154 | parsedCandidate.type = type.value(); |
| 155 | } else { |
| 156 | return absl::nullopt; |
| 157 | } |
| 158 |
| 159 | if (const auto tcpType = parseString(candidate.object_items(), "tcptype")) { |
| 160 | parsedCandidate.tcpType = tcpType.value(); |
| 161 | } |
| 162 |
| 163 | if (const auto relAddr = parseString(candidate.object_items(), "rel-addr")) { |
| 164 | parsedCandidate.relAddr = relAddr.value(); |
| 165 | } |
| 166 |
| 167 | if (const auto relPort = parseString(candidate.object_items(), "rel-port")) { |
| 168 | parsedCandidate.relPort = relPort.value(); |
| 169 | } |
| 170 |
| 171 | result.candidates.push_back(std::move(parsedCandidate)); |
| 172 | } |
| 173 |
| 174 | return result; |
| 175 | } |
| 176 |
| 177 | absl::optional<GroupJoinPayloadVideoPayloadType> parsePayloadType(json11::Json::object const &object) { |
| 178 | GroupJoinPayloadVideoPayloadType result; |
| 179 |
| 180 | if (const auto id = parseInt(object, "id")) { |
| 181 | result.id = (uint32_t)id.value(); |
| 182 | } else { |
| 183 | return absl::nullopt; |
| 184 | } |
| 185 |
| 186 | if (const auto name = parseString(object, "name")) { |
| 187 | result.name = name.value(); |
| 188 | } else { |
| 189 | return absl::nullopt; |
| 190 | } |
| 191 |
| 192 | if (const auto clockrate = parseInt(object, "clockrate")) { |
| 193 | result.clockrate = (uint32_t)clockrate.value(); |
| 194 | } else { |
| 195 | result.clockrate = 0; |
| 196 | } |
| 197 |
| 198 | if (const auto channels = parseInt(object, "channels")) { |
| 199 | result.channels = (uint32_t)channels.value(); |
| 200 | } else { |
| 201 | result.channels = 1; |
| 202 | } |
| 203 |
| 204 | const auto parameters = object.find("parameters"); |
| 205 | if (parameters != object.end() && parameters->second.is_object()) { |
| 206 | for (const auto ¶meter : parameters->second.object_items()) { |
| 207 | if (parameter.second.is_string()) { |
| 208 | result.parameters.push_back(std::make_pair(parameter.first, parameter.second.string_value())); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 |
| 213 | const auto rtcpFbs = object.find("rtcp-fbs"); |
| 214 | if (rtcpFbs != object.end() && rtcpFbs->second.is_array()) { |
| 215 | for (const auto &item : rtcpFbs->second.array_items()) { |
| 216 | if (item.is_object()) { |
| 217 | const auto type = item.object_items().find("type"); |
| 218 | if (type != item.object_items().end() && type->second.is_string()) { |
| 219 | GroupJoinPayloadVideoPayloadType::FeedbackType parsedFeedbackType; |
| 220 |
| 221 | const auto typeString = type->second.string_value(); |
| 222 |
| 223 | const auto subtype = item.object_items().find("subtype"); |
| 224 | if (subtype != item.object_items().end() && subtype->second.is_string()) { |
| 225 | parsedFeedbackType.type = typeString; |
| 226 | parsedFeedbackType.subtype = subtype->second.string_value(); |
| 227 | } else { |
| 228 | auto components = splitString(typeString, ' '); |
| 229 | if (components.size() == 1) { |
| 230 | parsedFeedbackType.type = components[0]; |
| 231 | } else if (components.size() == 2) { |
| 232 | parsedFeedbackType.type = components[0]; |
| 233 | parsedFeedbackType.subtype = components[1]; |
| 234 | } else { |
| 235 | continue; |
| 236 | } |
| 237 | } |
| 238 |
| 239 | result.feedbackTypes.push_back(std::move(parsedFeedbackType)); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 |
| 245 | return result; |
| 246 | } |
| 247 |
| 248 | absl::optional<GroupJoinVideoInformation> parseVideoInformation(json11::Json::object const &object) { |
| 249 | GroupJoinVideoInformation result; |
| 250 |
| 251 | const auto serverSources = object.find("server_sources"); |
| 252 | if (serverSources != object.end() && serverSources->second.is_array()) { |
| 253 | for (const auto &item : serverSources->second.array_items()) { |
| 254 | if (item.is_number()) { |
| 255 | int32_t value = item.int_value(); |
| 256 | uint32_t unsignedValue = *(uint32_t *)&value; |
| 257 | result.serverVideoBandwidthProbingSsrc = unsignedValue; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 |
| 262 | const auto payloadTypes = object.find("payload-types"); |
| 263 | if (payloadTypes != object.end() && payloadTypes->second.is_array()) { |
| 264 | for (const auto &payloadType : payloadTypes->second.array_items()) { |
| 265 | if (payloadType.is_object()) { |
| 266 | if (const auto parsedPayloadType = parsePayloadType(payloadType.object_items())) { |
| 267 | result.payloadTypes.push_back(parsedPayloadType.value()); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 |
| 273 | const auto rtpHdrexts = object.find("rtp-hdrexts"); |
| 274 | if (rtpHdrexts != object.end() && rtpHdrexts->second.is_array()) { |
| 275 | for (const auto &rtpHdrext : rtpHdrexts->second.array_items()) { |
| 276 | if (rtpHdrext.is_object()) { |
| 277 | const auto id = rtpHdrext.object_items().find("id"); |
| 278 | if (id == rtpHdrext.object_items().end() || !id->second.is_number()) { |
| 279 | continue; |
| 280 | } |
| 281 |
| 282 | const auto uri = rtpHdrext.object_items().find("uri"); |
| 283 | if (uri == rtpHdrext.object_items().end() || !uri->second.is_string()) { |
| 284 | continue; |
| 285 | } |
| 286 |
| 287 | result.extensionMap.push_back(std::make_pair(id->second.int_value(), uri->second.string_value())); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 |
| 292 | const auto endpointId = object.find("endpoint"); |
| 293 | if (endpointId != object.end() && endpointId->second.is_string()) { |
| 294 | result.endpointId = endpointId->second.string_value(); |
| 295 | } |
| 296 |
| 297 | return result; |
| 298 | } |
| 299 |
| 300 | } |
| 301 |
| 302 | std::string GroupJoinInternalPayload::serialize() { |
| 303 | json11::Json::object object; |
| 304 |
| 305 | int32_t signedSsrc = *(int32_t *)&audioSsrc; |
| 306 |
| 307 | object.insert(std::make_pair("ssrc", json11::Json(signedSsrc))); |
| 308 | object.insert(std::make_pair("ufrag", json11::Json(transport.ufrag))); |
| 309 | object.insert(std::make_pair("pwd", json11::Json(transport.pwd))); |
| 310 |
| 311 | json11::Json::array fingerprints; |
| 312 | for (const auto &fingerprint : transport.fingerprints) { |
| 313 | json11::Json::object fingerprintJson; |
| 314 |
| 315 | fingerprintJson.insert(std::make_pair("hash", json11::Json(fingerprint.hash))); |
| 316 | fingerprintJson.insert(std::make_pair("fingerprint", json11::Json(fingerprint.fingerprint))); |
| 317 | fingerprintJson.insert(std::make_pair("setup", json11::Json(fingerprint.setup))); |
| 318 |
| 319 | fingerprints.push_back(json11::Json(std::move(fingerprintJson))); |
| 320 | } |
| 321 | object.insert(std::make_pair("fingerprints", json11::Json(std::move(fingerprints)))); |
| 322 |
| 323 | if (videoInformation) { |
| 324 | json11::Json::array ssrcGroups; |
| 325 | for (const auto &ssrcGroup : videoInformation->ssrcGroups) { |
| 326 | json11::Json::object ssrcGroupJson; |
| 327 |
| 328 | json11::Json::array ssrcGroupSources; |
| 329 | for (auto ssrc : ssrcGroup.ssrcs) { |
| 330 | int32_t signedValue = *(int32_t *)&ssrc; |
| 331 | ssrcGroupSources.push_back(json11::Json(signedValue)); |
| 332 | } |
| 333 |
| 334 | ssrcGroupJson.insert(std::make_pair("sources", json11::Json(std::move(ssrcGroupSources)))); |
| 335 | ssrcGroupJson.insert(std::make_pair("semantics", json11::Json(ssrcGroup.semantics))); |
| 336 |
| 337 | ssrcGroups.push_back(json11::Json(std::move(ssrcGroupJson))); |
| 338 | } |
| 339 | object.insert(std::make_pair("ssrc-groups", json11::Json(std::move(ssrcGroups)))); |
| 340 | } |
| 341 |
| 342 | auto json = json11::Json(std::move(object)); |
| 343 | return json.dump(); |
| 344 | } |
| 345 |
| 346 | absl::optional<GroupJoinResponsePayload> GroupJoinResponsePayload::parse(std::string const &data) { |
| 347 | std::string parsingError; |
| 348 | auto json = json11::Json::parse(std::string(data.begin(), data.end()), parsingError); |
| 349 | if (json.type() != json11::Json::OBJECT) { |
| 350 | return absl::nullopt; |
| 351 | } |
| 352 |
| 353 | tgcalls::GroupJoinResponsePayload result; |
| 354 |
| 355 | const auto transport = json.object_items().find("transport"); |
| 356 | if (transport == json.object_items().end() || !transport->second.is_object()) { |
| 357 | return absl::nullopt; |
| 358 | } |
| 359 | if (const auto parsedTransport = parseTransportDescription(transport->second.object_items())) { |
| 360 | result.transport = parsedTransport.value(); |
| 361 | } else { |
| 362 | return absl::nullopt; |
| 363 | } |
| 364 |
| 365 | const auto video = json.object_items().find("video"); |
| 366 | if (video != json.object_items().end() && video->second.is_object()) { |
| 367 | result.videoInformation = parseVideoInformation(video->second.object_items()); |
| 368 | } |
| 369 |
| 370 | return result; |
| 371 | } |
| 372 |
| 373 | } |
| 374 |