| 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 | Serialize(to, serialized); |
| 76 | } |
| 77 |
| 78 | bool Deserialize(cricket::Candidate &to, rtc::ByteBufferReader &from) { |
| 79 | std::string candidate; |
| 80 | if (!Deserialize(candidate, from)) { |
| 81 | RTC_LOG(LS_ERROR) << "Could not read candidate string."; |
| 82 | return false; |
| 83 | } |
| 84 | webrtc::JsepIceCandidate parseCandidate{ std::string(), 0 }; |
| 85 | if (!parseCandidate.Initialize(candidate, nullptr)) { |
| 86 | RTC_LOG(LS_ERROR) << "Could not parse candidate: " << candidate; |
| 87 | return false; |
| 88 | } |
| 89 | to = parseCandidate.candidate(); |
| 90 | return true; |
| 91 | } |
| 92 |
| 93 | void Serialize(rtc::ByteBufferWriter &to, const RequestVideoMessage &from, bool singleMessagePacket) { |
| 94 | } |
| 95 |
| 96 | bool Deserialize(RequestVideoMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 97 | return true; |
| 98 | } |
| 99 |
| 100 | void Serialize(rtc::ByteBufferWriter &to, const RemoteMediaStateMessage &from, bool singleMessagePacket) { |
| 101 | uint8_t state = (uint8_t(from.video) << 1) | uint8_t(from.audio); |
| 102 | to.WriteUInt8(state); |
| 103 | } |
| 104 |
| 105 | bool Deserialize(RemoteMediaStateMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 106 | uint8_t state = 0; |
| 107 | if (!reader.ReadUInt8(&state)) { |
| 108 | RTC_LOG(LS_ERROR) << "Could not read remote media state."; |
| 109 | return false; |
| 110 | } |
| 111 | to.audio = AudioState(state & 0x01); |
| 112 | to.video = VideoState((state >> 1) & 0x03); |
| 113 | if (to.video == VideoState(0x03)) { |
| 114 | RTC_LOG(LS_ERROR) << "Invalid value for remote video state."; |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 |
| 120 | void Serialize(rtc::ByteBufferWriter &to, const CandidatesListMessage &from, bool singleMessagePacket) { |
| 121 | assert(from.candidates.size() < std::numeric_limits<uint8_t>::max()); |
| 122 |
| 123 | to.WriteUInt8(uint8_t(from.candidates.size())); |
| 124 | for (const auto &candidate : from.candidates) { |
| 125 | Serialize(to, candidate); |
| 126 | } |
| 127 |
| 128 | Serialize(to, from.iceParameters.ufrag); |
| 129 | Serialize(to, from.iceParameters.pwd); |
| 130 | } |
| 131 |
| 132 | bool Deserialize(CandidatesListMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 133 | auto count = uint8_t(); |
| 134 | if (!reader.ReadUInt8(&count)) { |
| 135 | RTC_LOG(LS_ERROR) << "Could not read candidates count."; |
| 136 | return false; |
| 137 | } |
| 138 | for (uint32_t i = 0; i != count; ++i) { |
| 139 | auto candidate = cricket::Candidate(); |
| 140 | if (!Deserialize(candidate, reader)) { |
| 141 | RTC_LOG(LS_ERROR) << "Could not read candidate."; |
| 142 | return false; |
| 143 | } |
| 144 | to.candidates.push_back(std::move(candidate)); |
| 145 | } |
| 146 | if (!Deserialize(to.iceParameters.ufrag, reader)) { |
| 147 | return false; |
| 148 | } |
| 149 | if (!Deserialize(to.iceParameters.pwd, reader)) { |
| 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 |
| 155 | void Serialize(rtc::ByteBufferWriter &to, const VideoFormatsMessage &from, bool singleMessagePacket) { |
| 156 | assert(from.formats.size() < std::numeric_limits<uint8_t>::max()); |
| 157 | assert(from.encodersCount <= from.formats.size()); |
| 158 |
| 159 | to.WriteUInt8(uint8_t(from.formats.size())); |
| 160 | for (const auto &format : from.formats) { |
| 161 | Serialize(to, format); |
| 162 | } |
| 163 | to.WriteUInt8(uint8_t(from.encodersCount)); |
| 164 | } |
| 165 |
| 166 | bool Deserialize(VideoFormatsMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 167 | auto count = uint8_t(); |
| 168 | if (!from.ReadUInt8(&count)) { |
| 169 | RTC_LOG(LS_ERROR) << "Could not read video formats count."; |
| 170 | return false; |
| 171 | } |
| 172 | for (uint32_t i = 0; i != count; ++i) { |
| 173 | auto format = webrtc::SdpVideoFormat(std::string()); |
| 174 | if (!Deserialize(format, from)) { |
| 175 | RTC_LOG(LS_ERROR) << "Could not read video format."; |
| 176 | return false; |
| 177 | } |
| 178 | to.formats.push_back(std::move(format)); |
| 179 | } |
| 180 | auto encoders = uint8_t(); |
| 181 | if (!from.ReadUInt8(&encoders)) { |
| 182 | RTC_LOG(LS_ERROR) << "Could not read encoders count."; |
| 183 | return false; |
| 184 | } else if (encoders > to.formats.size()) { |
| 185 | RTC_LOG(LS_ERROR) << "Invalid encoders count: " << encoders << ", full formats count: " << to.formats.size(); |
| 186 | return false; |
| 187 | } |
| 188 | to.encodersCount = encoders; |
| 189 | return true; |
| 190 | } |
| 191 |
| 192 | void Serialize(rtc::ByteBufferWriter &to, const rtc::CopyOnWriteBuffer &from, bool singleMessagePacket) { |
| 193 | if (!singleMessagePacket) { |
| 194 | assert(from.size() <= UINT16_MAX); |
| 195 | to.WriteUInt16(from.size()); |
| 196 | } |
| 197 | to.WriteBytes(reinterpret_cast<const char*>(from.cdata()), from.size()); |
| 198 | } |
| 199 |
| 200 | bool Deserialize(rtc::CopyOnWriteBuffer &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 201 | auto length = uint16_t(from.Length()); |
| 202 | if (!singleMessagePacket) { |
| 203 | if (!from.ReadUInt16(&length)) { |
| 204 | RTC_LOG(LS_ERROR) << "Could not read buffer length."; |
| 205 | return false; |
| 206 | } else if (from.Length() < length) { |
| 207 | RTC_LOG(LS_ERROR) << "Invalid buffer length: " << length << ", available: " << from.Length(); |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | to.AppendData(from.Data(), length); |
| 212 | from.Consume(length); |
| 213 | return true; |
| 214 | } |
| 215 |
| 216 | void Serialize(rtc::ByteBufferWriter &to, const AudioDataMessage &from, bool singleMessagePacket) { |
| 217 | Serialize(to, from.data, singleMessagePacket); |
| 218 | } |
| 219 |
| 220 | bool Deserialize(AudioDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 221 | return Deserialize(to.data, from, singleMessagePacket); |
| 222 | } |
| 223 |
| 224 | void Serialize(rtc::ByteBufferWriter &to, const VideoDataMessage &from, bool singleMessagePacket) { |
| 225 | Serialize(to, from.data, singleMessagePacket); |
| 226 | } |
| 227 |
| 228 | bool Deserialize(VideoDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 229 | return Deserialize(to.data, from, singleMessagePacket); |
| 230 | } |
| 231 |
| 232 | void Serialize(rtc::ByteBufferWriter &to, const UnstructuredDataMessage &from, bool singleMessagePacket) { |
| 233 | Serialize(to, from.data, singleMessagePacket); |
| 234 | } |
| 235 |
| 236 | bool Deserialize(UnstructuredDataMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 237 | return Deserialize(to.data, from, singleMessagePacket); |
| 238 | } |
| 239 |
| 240 | void Serialize(rtc::ByteBufferWriter &to, const VideoParametersMessage &from, bool singleMessagePacket) { |
| 241 | to.WriteUInt32(from.aspectRatio); |
| 242 | } |
| 243 |
| 244 | bool Deserialize(VideoParametersMessage &to, rtc::ByteBufferReader &from, bool singleMessagePacket) { |
| 245 | uint32_t aspectRatio = 0; |
| 246 | if (!from.ReadUInt32(&aspectRatio)) { |
| 247 | return false; |
| 248 | } |
| 249 | to.aspectRatio = aspectRatio; |
| 250 | return true; |
| 251 | } |
| 252 |
| 253 | void Serialize(rtc::ByteBufferWriter &to, const RemoteBatteryLevelIsLowMessage &from, bool singleMessagePacket) { |
| 254 | to.WriteUInt8(from.batteryLow ? 1 : 0); |
| 255 | } |
| 256 |
| 257 | bool Deserialize(RemoteBatteryLevelIsLowMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 258 | uint8_t value = 0; |
| 259 | if (!reader.ReadUInt8(&value)) { |
| 260 | RTC_LOG(LS_ERROR) << "Could not read batteryLevelIsLow."; |
| 261 | return false; |
| 262 | } |
| 263 | to.batteryLow = (value != 0); |
| 264 | return true; |
| 265 | } |
| 266 |
| 267 | void Serialize(rtc::ByteBufferWriter &to, const RemoteNetworkTypeMessage &from, bool singleMessagePacket) { |
| 268 | to.WriteUInt8(from.isLowCost ? 1 : 0); |
| 269 | } |
| 270 |
| 271 | bool Deserialize(RemoteNetworkTypeMessage &to, rtc::ByteBufferReader &reader, bool singleMessagePacket) { |
| 272 | uint8_t value = 0; |
| 273 | if (!reader.ReadUInt8(&value)) { |
| 274 | RTC_LOG(LS_ERROR) << "Could not read isLowCost."; |
| 275 | return false; |
| 276 | } |
| 277 | to.isLowCost = (value != 0); |
| 278 | return true; |
| 279 | } |
| 280 |
| 281 | enum class TryResult : uint8_t { |
| 282 | Success, |
| 283 | TryNext, |
| 284 | Abort, |
| 285 | }; |
| 286 |
| 287 | template <typename T> |
| 288 | TryResult TryDeserialize( |
| 289 | absl::optional<Message> &to, |
| 290 | rtc::ByteBufferReader &reader, |
| 291 | bool singleMessagePacket) { |
| 292 | assert(reader.Length() != 0); |
| 293 |
| 294 | constexpr auto id = T::kId; |
| 295 | if (uint8_t(*reader.Data()) != id) { |
| 296 | return TryResult::TryNext; |
| 297 | } |
| 298 | reader.Consume(1); |
| 299 | auto parsed = T(); |
| 300 | if (!Deserialize(parsed, reader, singleMessagePacket)) { |
| 301 | RTC_LOG(LS_ERROR) << "Could not read message with kId: " << id; |
| 302 | return TryResult::Abort; |
| 303 | } |
| 304 | to = Message{ parsed }; |
| 305 | return TryResult::Success; |
| 306 | } |
| 307 |
| 308 | template <typename ...Types> |
| 309 | struct TryDeserializeNext; |
| 310 |
| 311 | template <> |
| 312 | struct TryDeserializeNext<> { |
| 313 | static bool Call( |
| 314 | absl::optional<Message> &to, |
| 315 | rtc::ByteBufferReader &reader, |
| 316 | bool singleMessagePacket) { |
| 317 | return false; |
| 318 | } |
| 319 | }; |
| 320 |
| 321 | template <typename T, typename ...Other> |
| 322 | struct TryDeserializeNext<T, Other...> { |
| 323 | static bool Call( |
| 324 | absl::optional<Message> &to, |
| 325 | rtc::ByteBufferReader &reader, |
| 326 | bool singleMessagePacket) { |
| 327 | const auto result = TryDeserialize<T>(to, reader, singleMessagePacket); |
| 328 | return (result == TryResult::TryNext) |
| 329 | ? TryDeserializeNext<Other...>::Call(to, reader, singleMessagePacket) |
| 330 | : (result == TryResult::Success); |
| 331 | } |
| 332 | }; |
| 333 |
| 334 | template <typename ...Types> |
| 335 | bool TryDeserializeRecursive( |
| 336 | absl::optional<Message> &to, |
| 337 | rtc::ByteBufferReader &reader, |
| 338 | bool singleMessagePacket, |
| 339 | absl::variant<Types...> *) { |
| 340 | return TryDeserializeNext<Types...>::Call(to, reader, singleMessagePacket); |
| 341 | } |
| 342 |
| 343 | } // namespace |
| 344 |
| 345 |
| 346 | rtc::CopyOnWriteBuffer SerializeMessageWithSeq( |
| 347 | const Message &message, |
| 348 | uint32_t seq, |
| 349 | bool singleMessagePacket) { |
| 350 | rtc::ByteBufferWriter writer; |
| 351 | writer.WriteUInt32(seq); |
| 352 | absl::visit([&](const auto &data) { |
| 353 | writer.WriteUInt8(std::decay_t<decltype(data)>::kId); |
| 354 | Serialize(writer, data, singleMessagePacket); |
| 355 | }, message.data); |
| 356 |
| 357 | auto result = rtc::CopyOnWriteBuffer(); |
| 358 | result.AppendData(writer.Data(), writer.Length()); |
| 359 |
| 360 | return result; |
| 361 | } |
| 362 |
| 363 | absl::optional<Message> DeserializeMessage( |
| 364 | rtc::ByteBufferReader &reader, |
| 365 | bool singleMessagePacket) { |
| 366 | if (!reader.Length()) { |
| 367 | return absl::nullopt; |
| 368 | } |
| 369 | using Variant = decltype(std::declval<Message>().data); |
| 370 | auto result = absl::make_optional<Message>(); |
| 371 | return TryDeserializeRecursive(result, reader, singleMessagePacket, (Variant*)nullptr) |
| 372 | ? result |
| 373 | : absl::nullopt; |
| 374 | } |
| 375 |
| 376 | } // namespace tgcalls |
| 377 |