| 6 | namespace tgcalls { |
| 7 | namespace { |
| 8 |
| 9 | constexpr auto kMaxStringLength = 65536; |
| 10 |
| 11 | void Serialize(rtc::ByteBufferWriter &to, const std::string &from) { |
| 12 | assert(from.size() < kMaxStringLength); |
| 13 |
| 14 | to.WriteUInt32(uint32_t(from.size())); |
| 15 | to.WriteString(from); |
| 16 | } |
| 17 |
| 18 | bool Deserialize(std::string &to, rtc::ByteBufferReader &from) { |
| 19 | uint32_t length = 0; |
| 20 | if (!from.ReadUInt32(&length)) { |
| 21 | RTC_LOG(LS_ERROR) << "Could not read string length."; |
| 22 | return false; |
| 23 | } else if (length >= kMaxStringLength) { |
| 24 | RTC_LOG(LS_ERROR) << "Invalid string length: " << length; |
| 25 | return false; |
| 26 | } else if (!from.ReadString(&to, length)) { |
| 27 | RTC_LOG(LS_ERROR) << "Could not read string data."; |
| 28 | return false; |
| 29 | } |
| 30 | return true; |
| 31 | } |
| 32 |
| 33 | void Serialize(rtc::ByteBufferWriter &to, const webrtc::SdpVideoFormat &from) { |
| 34 | assert(from.parameters.size() < std::numeric_limits<uint8_t>::max()); |
| 35 |
| 36 | Serialize(to, from.name); |
| 37 | to.WriteUInt8(uint8_t(from.parameters.size())); |
| 38 | for (const auto &pair : from.parameters) { |
| 39 | Serialize(to, pair.first); |
| 40 | Serialize(to, pair.second); |
| 41 | } |
| 42 | } |
| 43 |
| 44 | bool Deserialize(webrtc::SdpVideoFormat &to, rtc::ByteBufferReader &from) { |
| 45 | if (!Deserialize(to.name, from)) { |
| 46 | RTC_LOG(LS_ERROR) << "Could not read video format name."; |
| 47 | return false; |
| 48 | } |
| 49 | auto count = uint8_t(); |
| 50 | if (!from.ReadUInt8(&count)) { |
| 51 | RTC_LOG(LS_ERROR) << "Could not read video format parameters count."; |
| 52 | return false; |
| 53 | } |
| 54 | for (uint32_t i = 0; i != count; ++i) { |
| 55 | auto key = std::string(); |
| 56 | auto value = std::string(); |
| 57 | if (!Deserialize(key, from)) { |
| 58 | RTC_LOG(LS_ERROR) << "Could not read video format parameter key."; |
| 59 | return false; |
| 60 | } else if (!Deserialize(value, from)) { |
| 61 | RTC_LOG(LS_ERROR) << "Could not read video format parameter value."; |
| 62 | return false; |
| 63 | } |
| 64 | to.parameters.emplace(key, value); |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 |
| 69 | void Serialize(rtc::ByteBufferWriter &to, const cricket::Candidate &from) { |
| 70 | webrtc::JsepIceCandidate iceCandidate{ std::string(), 0 }; |
| 71 | iceCandidate.SetCandidate(from); |
| 72 | std::string serialized; |
| 73 | const auto success = iceCandidate.ToString(&serialized); |
| 74 | assert(success); |
| 75 | (void)success; |
| 76 | Serialize(to, serialized); |
| 77 | } |
| 78 |
| 79 | bool Deserialize(cricket::Candidate &to, rtc::ByteBufferReader &from) { |
| 80 | std::string candidate; |
| 81 | if (!Deserialize(candidate, from)) { |
| 82 | RTC_LOG(LS_ERROR) << "Could not read candidate string."; |
| 83 | return false; |
| 84 | } |
| 85 | webrtc::JsepIceCandidate parseCandidate{ std::string(), 0 }; |
| 86 | if (!parseCandidate.Initialize(candidate, nullptr)) { |
| 87 | RTC_LOG(LS_ERROR) << "Could not parse candidate: " << candidate; |
| 88 | return false; |
| 89 | } |
| 90 | to = parseCandidate.candidate(); |
| 91 | return true; |
| 92 | } |
| 93 |
| 94 | void Serialize(rtc::ByteBufferWriter &to, const RequestVideoMessage &from, bool singleMessagePacket) { |
| 95 | } |
| 96 |
| 97 | bool Deserialize(RequestVideoMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 98 | return true; |
| 99 | } |
| 100 |
| 101 | void Serialize(rtc::ByteBufferWriter &to, const RemoteMediaStateMessage &from, bool singleMessagePacket) { |
| 102 | uint8_t state = (uint8_t(from.video) << 1) | uint8_t(from.audio); |
| 103 | to.WriteUInt8(state); |
| 104 | } |
| 105 |
| 106 | bool Deserialize(RemoteMediaStateMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 107 | uint8_t state = 0; |
| 108 | if (!reader.ReadUInt8(&state)) { |
| 109 | RTC_LOG(LS_ERROR) << "Could not read remote media state."; |
| 110 | return false; |
| 111 | } |
| 112 | to.audio = AudioState(state & 0x01); |
| 113 | to.video = VideoState((state >> 1) & 0x03); |
| 114 | if (to.video == VideoState(0x03)) { |
| 115 | RTC_LOG(LS_ERROR) << "Invalid value for remote video state."; |
| 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 |
| 121 | void Serialize(rtc::ByteBufferWriter &to, const CandidatesListMessage &from, bool singleMessagePacket) { |
| 122 | assert(from.candidates.size() < std::numeric_limits<uint8_t>::max()); |
| 123 |
| 124 | to.WriteUInt8(uint8_t(from.candidates.size())); |
| 125 | for (const auto &candidate : from.candidates) { |
| 126 | Serialize(to, candidate); |
| 127 | } |
| 128 |
| 129 | Serialize(to, from.iceParameters.ufrag); |
| 130 | Serialize(to, from.iceParameters.pwd); |
| 131 | } |
| 132 |
| 133 | bool Deserialize(CandidatesListMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 134 | auto count = uint8_t(); |
| 135 | if (!reader.ReadUInt8(&count)) { |
| 136 | RTC_LOG(LS_ERROR) << "Could not read candidates count."; |
| 137 | return false; |
| 138 | } |
| 139 | for (uint32_t i = 0; i != count; ++i) { |
| 140 | auto candidate = cricket::Candidate(); |
| 141 | if (!Deserialize(candidate, reader)) { |
| 142 | RTC_LOG(LS_ERROR) << "Could not read candidate."; |
| 143 | return false; |
| 144 | } |
| 145 | to.candidates.push_back(std::move(candidate)); |
| 146 | } |
| 147 | if (!Deserialize(to.iceParameters.ufrag, reader)) { |
| 148 | return false; |
| 149 | } |
| 150 | if (!Deserialize(to.iceParameters.pwd, reader)) { |
| 151 | return false; |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 |
| 156 | void Serialize(rtc::ByteBufferWriter &to, const VideoFormatsMessage &from, bool singleMessagePacket) { |
| 157 | assert(from.formats.size() < std::numeric_limits<uint8_t>::max()); |
| 158 | assert(from.encodersCount <= from.formats.size()); |
| 159 |
| 160 | to.WriteUInt8(uint8_t(from.formats.size())); |
| 161 | for (const auto &format : from.formats) { |
| 162 | Serialize(to, format); |
| 163 | } |
| 164 | to.WriteUInt8(uint8_t(from.encodersCount)); |
| 165 | } |
| 166 |
| 167 | bool Deserialize(VideoFormatsMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 168 | auto count = uint8_t(); |
| 169 | if (!from.ReadUInt8(&count)) { |
| 170 | RTC_LOG(LS_ERROR) << "Could not read video formats count."; |
| 171 | return false; |
| 172 | } |
| 173 | for (uint32_t i = 0; i != count; ++i) { |
| 174 | auto format = webrtc::SdpVideoFormat(std::string()); |
| 175 | if (!Deserialize(format, from)) { |
| 176 | RTC_LOG(LS_ERROR) << "Could not read video format."; |
| 177 | return false; |
| 178 | } |
| 179 | to.formats.push_back(std::move(format)); |
| 180 | } |
| 181 | auto encoders = uint8_t(); |
| 182 | if (!from.ReadUInt8(&encoders)) { |
| 183 | RTC_LOG(LS_ERROR) << "Could not read encoders count."; |
| 184 | return false; |
| 185 | } else if (encoders > to.formats.size()) { |
| 186 | RTC_LOG(LS_ERROR) << "Invalid encoders count: " << encoders << ", full formats count: " << to.formats.size(); |
| 187 | return false; |
| 188 | } |
| 189 | to.encodersCount = encoders; |
| 190 | return true; |
| 191 | } |
| 192 |
| 193 | void Serialize(rtc::ByteBufferWriter &to, const rtc::CopyOnWriteBuffer &from, bool singleMessagePacket) { |
| 194 | if (!singleMessagePacket) { |
| 195 | assert(from.size() <= UINT16_MAX); |
| 196 | to.WriteUInt16(from.size()); |
| 197 | } |
| 198 | to.WriteBytes(reinterpret_cast<const uint8_t*>(from.cdata()), from.size()); |
| 199 | } |
| 200 |
| 201 | bool Deserialize(rtc::CopyOnWriteBuffer &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 202 | auto length = uint16_t(from.Length()); |
| 203 | if (!singleMessagePacket) { |
| 204 | if (!from.ReadUInt16(&length)) { |
| 205 | RTC_LOG(LS_ERROR) << "Could not read buffer length."; |
| 206 | return false; |
| 207 | } else if (from.Length() < length) { |
| 208 | RTC_LOG(LS_ERROR) << "Invalid buffer length: " << length << ", available: " << from.Length(); |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | to.AppendData(from.Data(), length); |
| 213 | from.Consume(length); |
| 214 | return true; |
| 215 | } |
| 216 |
| 217 | void Serialize(rtc::ByteBufferWriter &to, const AudioDataMessage &from, bool singleMessagePacket) { |
| 218 | Serialize(to, from.data, singleMessagePacket); |
| 219 | } |
| 220 |
| 221 | bool Deserialize(AudioDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 222 | return Deserialize(to.data, from, singleMessagePacket); |
| 223 | } |
| 224 |
| 225 | void Serialize(rtc::ByteBufferWriter &to, const VideoDataMessage &from, bool singleMessagePacket) { |
| 226 | Serialize(to, from.data, singleMessagePacket); |
| 227 | } |
| 228 |
| 229 | bool Deserialize(VideoDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 230 | return Deserialize(to.data, from, singleMessagePacket); |
| 231 | } |
| 232 |
| 233 | void Serialize(rtc::ByteBufferWriter &to, const UnstructuredDataMessage &from, bool singleMessagePacket) { |
| 234 | Serialize(to, from.data, singleMessagePacket); |
| 235 | } |
| 236 |
| 237 | bool Deserialize(UnstructuredDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 238 | return Deserialize(to.data, from, singleMessagePacket); |
| 239 | } |
| 240 |
| 241 | void Serialize(rtc::ByteBufferWriter &to, const VideoParametersMessage &from, bool singleMessagePacket) { |
| 242 | to.WriteUInt32(from.aspectRatio); |
| 243 | } |
| 244 |
| 245 | bool Deserialize(VideoParametersMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 246 | uint32_t aspectRatio = 0; |
| 247 | if (!from.ReadUInt32(&aspectRatio)) { |
| 248 | return false; |
| 249 | } |
| 250 | to.aspectRatio = aspectRatio; |
| 251 | return true; |
| 252 | } |
| 253 |
| 254 | void Serialize(rtc::ByteBufferWriter &to, const RemoteBatteryLevelIsLowMessage &from, bool singleMessagePacket) { |
| 255 | to.WriteUInt8(from.batteryLow ? 1 : 0); |
| 256 | } |
| 257 |
| 258 | bool Deserialize(RemoteBatteryLevelIsLowMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 259 | uint8_t value = 0; |
| 260 | if (!reader.ReadUInt8(&value)) { |
| 261 | RTC_LOG(LS_ERROR) << "Could not read batteryLevelIsLow."; |
| 262 | return false; |
| 263 | } |
| 264 | to.batteryLow = (value != 0); |
| 265 | return true; |
| 266 | } |
| 267 |
| 268 | void Serialize(rtc::ByteBufferWriter &to, const RemoteNetworkStatusMessage &from, bool singleMessagePacket) { |
| 269 | to.WriteUInt8(from.isLowCost ? 1 : 0); |
| 270 | to.WriteUInt8(from.isLowDataRequested ? 1 : 0); |
| 271 | } |
| 272 |
| 273 | bool Deserialize(RemoteNetworkStatusMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 274 | uint8_t value = 0; |
| 275 | if (!reader.ReadUInt8(&value)) { |
| 276 | RTC_LOG(LS_ERROR) << "Could not read isLowCost."; |
| 277 | return false; |
| 278 | } |
| 279 | to.isLowCost = (value != 0); |
| 280 | if (reader.ReadUInt8(&value)) { |
| 281 | to.isLowDataRequested = (value != 0); |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 |
| 286 | enum class TryResult : uint8_t { |
| 287 | Success, |
| 288 | TryNext, |
| 289 | Abort, |
| 290 | }; |
| 291 |
| 292 | template <typename T> |
| 293 | TryResult TryDeserialize( |
| 294 | absl::optional<Message> &to, |
| 295 | rtc::ByteBufferReader &reader, |
| 296 | bool singleMessagePacket) { |
| 297 | assert(reader.Length() != 0); |
| 298 |
| 299 | constexpr auto id = T::kId; |
| 300 | if (uint8_t(*reader.Data()) != id) { |
| 301 | return TryResult::TryNext; |
| 302 | } |
| 303 | reader.Consume(1); |
| 304 | auto parsed = T(); |
| 305 | if (!Deserialize(parsed, reader, singleMessagePacket)) { |
| 306 | RTC_LOG(LS_ERROR) << "Could not read message with kId: " << id; |
| 307 | return TryResult::Abort; |
| 308 | } |
| 309 | to = Message{ parsed }; |
| 310 | return TryResult::Success; |
| 311 | } |
| 312 |
| 313 | template <typename ...Types> |
| 314 | struct TryDeserializeNext; |
| 315 |
| 316 | template <> |
| 317 | struct TryDeserializeNext<> { |
| 318 | static bool Call( |
| 319 | absl::optional<Message> &to, |
| 320 | rtc::ByteBufferReader &reader, |
| 321 | bool singleMessagePacket) { |
| 322 | return false; |
| 323 | } |
| 324 | }; |
| 325 |
| 326 | template <typename T, typename ...Other> |
| 327 | struct TryDeserializeNext<T, Other...> { |
| 328 | static bool Call( |
| 329 | absl::optional<Message> &to, |
| 330 | rtc::ByteBufferReader &reader, |
| 331 | bool singleMessagePacket) { |
| 332 | const auto result = TryDeserialize<T>(to, reader, singleMessagePacket); |
| 333 | return (result == TryResult::TryNext) |
| 334 | ? TryDeserializeNext<Other...>::Call(to, reader, singleMessagePacket) |
| 335 | : (result == TryResult::Success); |
| 336 | } |
| 337 | }; |
| 338 |
| 339 | template <typename ...Types> |
| 340 | bool TryDeserializeRecursive( |
| 341 | absl::optional<Message> &to, |
| 342 | rtc::ByteBufferReader &reader, |
| 343 | bool singleMessagePacket, |
| 344 | absl::variant<Types...> *) { |
| 345 | return TryDeserializeNext<Types...>::Call(to, reader, singleMessagePacket); |
| 346 | } |
| 347 |
| 348 | } // namespace |
| 349 |
| 350 |
| 351 | rtc::CopyOnWriteBuffer SerializeMessageWithSeq( |
| 352 | const Message &message, |
| 353 | uint32_t seq, |
| 354 | bool singleMessagePacket) { |
| 355 | rtc::ByteBufferWriter writer; |
| 356 | writer.WriteUInt32(seq); |
| 357 | absl::visit([&](const auto &data) { |
| 358 | writer.WriteUInt8(std::decay_t<decltype(data)>::kId); |
| 359 | Serialize(writer, data, singleMessagePacket); |
| 360 | }, message.data); |
| 361 |
| 362 | auto result = rtc::CopyOnWriteBuffer(); |
| 363 | result.AppendData(writer.Data(), writer.Length()); |
| 364 |
| 365 | return result; |
| 366 | } |
| 367 |
| 368 | absl::optional<Message> DeserializeMessage( |
| 369 | rtc::ByteBufferReader &reader, |
| 370 | bool singleMessagePacket) { |
| 371 | if (!reader.Length()) { |
| 372 | return absl::nullopt; |
| 373 | } |
| 374 | using Variant = decltype(std::declval<Message>().data); |
| 375 | auto result = absl::make_optional<Message>(); |
| 376 | return TryDeserializeRecursive(result, reader, singleMessagePacket, (Variant*)nullptr) |
| 377 | ? result |
| 378 | : absl::nullopt; |
| 379 | } |
| 380 |
| 381 | absl::optional<rtc::CopyOnWriteBuffer> DeserializeRawMessage( |
| 382 | rtc::ByteBufferReader &reader, |
| 383 | bool singleMessagePacket) { |
| 384 | if (!reader.Length()) { |
| 385 | return absl::nullopt; |
| 386 | } |
| 387 |
| 388 | uint32_t length = 0; |
| 389 | if (!reader.ReadUInt32(&length)) { |
| 390 | return absl::nullopt; |
| 391 | } |
| 392 |
| 393 | if (/*length < 0 || */length > 1024 * 1024) { |
| 394 | return absl::nullopt; |
| 395 | } |
| 396 |
| 397 | rtc::CopyOnWriteBuffer result; |
| 398 | result.SetSize(length); |
| 399 | if (!reader.ReadBytes(rtc::ArrayView<uint8_t>((uint8_t *)result.MutableData(), result.size()))) { |
| 400 | return absl::nullopt; |
| 401 | } |
| 402 |
| 403 | return result; |
| 404 | } |
| 405 |
| 406 | } // namespace tgcalls |
| 407 |