| #include "p2p/base/p2p_transport_channel.h" |
| 6 | #include "p2p/base/basic_async_resolver_factory.h" |
| 7 | #include "api/packet_socket_factory.h" |
| 8 | #include "rtc_base/rtc_certificate_generator.h" |
| 9 | #include "p2p/base/ice_credentials_iterator.h" |
| 10 | #include "api/jsep_ice_candidate.h" |
| 11 | #include "p2p/base/dtls_transport.h" |
| 12 | #include "p2p/base/dtls_transport_factory.h" |
| 13 | #include "pc/dtls_srtp_transport.h" |
| 14 | #include "pc/dtls_transport.h" |
| 15 | #include "pc/jsep_transport_controller.h" |
| 16 | #include "api/async_dns_resolver.h" |
| 17 |
| 18 | #include "TurnCustomizerImpl.h" |
| 19 | #include "ReflectorRelayPortFactory.h" |
| 20 | #include "SctpDataChannelProviderInterfaceImpl.h" |
| 21 | #include "StaticThreads.h" |
| 22 | #include "platform/PlatformInterface.h" |
| 23 | #include "p2p/base/turn_port.h" |
| 24 |
| 25 | #include "ReflectorPort.h" |
| 26 | #include "FieldTrialsConfig.h" |
| 27 |
| 28 | namespace tgcalls { |
| 29 |
| 30 | namespace { |
| 31 |
| 32 | } |
| 33 |
| 34 | class DirectPacketTransport : public rtc::PacketTransportInternal, public std::enable_shared_from_this<DirectPacketTransport> { |
| 35 | public: |
| 36 | DirectPacketTransport( |
| 37 | rtc::Thread *thread, |
| 38 | EncryptionKey const &encryptionKey, |
| 39 | std::shared_ptr<DirectConnectionChannel> channel, std::function<void(bool)> &&isConnectedUpdated |
| 40 | ) : |
| 41 | _isConnectedUpdated(std::move(isConnectedUpdated)), |
| 42 | _thread(thread), |
| 43 | _encryption( |
| 44 | EncryptedConnection::Type::Transport, |
| 45 | encryptionKey, |
| 46 | [=](int delayMs, int cause) { |
| 47 | assert(false); |
| 48 | }), |
| 49 | _channel(channel) { |
| 50 | assert(_thread->IsCurrent()); |
| 51 | } |
| 52 |
| 53 | virtual ~DirectPacketTransport() { |
| 54 | } |
| 55 |
| 56 | void start() { |
| 57 | auto weakSelf = std::weak_ptr<DirectPacketTransport>(shared_from_this()); |
| 58 | _onIncomingPacketToken = _channel->addOnIncomingPacket([weakSelf, thread = _thread](std::shared_ptr<std::vector<uint8_t>> packet) { |
| 59 | thread->PostTask([weakSelf, packet] { |
| 60 | auto strongSelf = weakSelf.lock(); |
| 61 | if (!strongSelf) { |
| 62 | return; |
| 63 | } |
| 64 | strongSelf->processIncomingPacket(packet); |
| 65 | }); |
| 66 | }); |
| 67 |
| 68 | updateState(); |
| 69 | runStateTimer(); |
| 70 | } |
| 71 |
| 72 | void stop() { |
| 73 | if (!_onIncomingPacketToken.empty()) { |
| 74 | _channel->removeOnIncomingPacket(_onIncomingPacketToken); |
| 75 | _onIncomingPacketToken.resize(0); |
| 76 | } |
| 77 | } |
| 78 |
| 79 | public: |
| 80 | virtual const std::string &transport_name() const override { |
| 81 | return name; |
| 82 | } |
| 83 |
| 84 | // The transport has been established. |
| 85 | virtual bool writable() const override { |
| 86 | return _isConnected; |
| 87 | } |
| 88 |
| 89 | // The transport has received a packet in the last X milliseconds, here X is |
| 90 | // configured by each implementation. |
| 91 | virtual bool receiving() const override { |
| 92 | return _isConnected; |
| 93 | } |
| 94 |
| 95 | // Attempts to send the given packet. |
| 96 | // The return value is < 0 on failure. The return value in failure case is not |
| 97 | // descriptive. Depending on failure cause and implementation details |
| 98 | // GetError() returns an descriptive errno.h error value. |
| 99 | // This mimics posix socket send() or sendto() behavior. |
| 100 | // TODO(johan): Reliable, meaningful, consistent error codes for all |
| 101 | // implementations would be nice. |
| 102 | // TODO(johan): Remove the default argument once channel code is updated. |
| 103 | virtual int SendPacket(const char *data, |
| 104 | size_t len, |
| 105 | const rtc::PacketOptions& options, |
| 106 | int flags = 0) override { |
| 107 | if (!_isConnected) { |
| 108 | _lastError = ENOTCONN; |
| 109 | return -1; |
| 110 | } |
| 111 |
| 112 | rtc::CopyOnWriteBuffer buffer; |
| 113 | buffer.AppendData(data, len); |
| 114 |
| 115 | Message message = flags == 0 ? Message { AudioDataMessage { buffer } } : Message { VideoDataMessage { buffer } }; |
| 116 |
| 117 | if (const auto prepared = _encryption.prepareForSending(message)) { |
| 118 | rtc::PacketOptions packetOptions; |
| 119 |
| 120 | rtc::ByteBufferWriter bufferWriter; |
| 121 | bufferWriter.WriteUInt32((uint32_t)prepared->bytes.size()); |
| 122 | bufferWriter.WriteBytes(reinterpret_cast<const uint8_t *>(prepared->bytes.data()), prepared->bytes.size()); |
| 123 | while (bufferWriter.Length() % 4 != 0) { |
| 124 | bufferWriter.WriteUInt8(0); |
| 125 | } |
| 126 |
| 127 | auto packet = std::make_unique<std::vector<uint8_t>>(bufferWriter.Data(), bufferWriter.Data() + bufferWriter.Length()); |
| 128 | _channel->sendPacket(std::move(packet)); |
| 129 | } |
| 130 |
| 131 | rtc::SentPacket sentPacket; |
| 132 | sentPacket.packet_id = options.packet_id; |
| 133 | sentPacket.send_time_ms = rtc::TimeMillis(); |
| 134 | SignalSentPacket(this, sentPacket); |
| 135 |
| 136 | return 0; |
| 137 | } |
| 138 |
| 139 | // Sets a socket option. Note that not all options are |
| 140 | // supported by all transport types. |
| 141 | virtual int SetOption(rtc::Socket::Option opt, int value) override { |
| 142 | return 0; |
| 143 | } |
| 144 |
| 145 | // TODO(pthatcher): Once Chrome's MockPacketTransportInterface implements |
| 146 | // this, remove the default implementation. |
| 147 | virtual bool GetOption(rtc::Socket::Option opt, int *value) override { |
| 148 | return false; |
| 149 | } |
| 150 |
| 151 | // Returns the most recent error that occurred on this channel. |
| 152 | virtual int GetError() override { |
| 153 | return _lastError; |
| 154 | } |
| 155 |
| 156 | // Returns the current network route with transport overhead. |
| 157 | // TODO(zhihuang): Make it pure virtual once the Chrome/remoting is updated. |
| 158 | virtual absl::optional<rtc::NetworkRoute> network_route() const override { |
| 159 | return absl::nullopt; |
| 160 | } |
| 161 |
| 162 | /*sigslot::signal1<PacketTransportInternal*> SignalWritableState; |
| 163 |
| 164 | // Emitted when the PacketTransportInternal is ready to send packets. "Ready |
| 165 | // to send" is more sensitive than the writable state; a transport may be |
| 166 | // writable, but temporarily not able to send packets. For example, the |
| 167 | // underlying transport's socket buffer may be full, as indicated by |
| 168 | // SendPacket's return code and/or GetError. |
| 169 | sigslot::signal1<PacketTransportInternal*> SignalReadyToSend; |
| 170 |
| 171 | // Emitted when receiving state changes to true. |
| 172 | sigslot::signal1<PacketTransportInternal*> SignalReceivingState; |
| 173 |
| 174 | // Signalled each time a packet is received on this channel. |
| 175 | sigslot::signal5<PacketTransportInternal*, |
| 176 | const char*, |
| 177 | size_t, |
| 178 | // TODO(bugs.webrtc.org/9584): Change to passing the int64_t |
| 179 | // timestamp by value. |
| 180 | const int64_t&, |
| 181 | int> |
| 182 | SignalReadPacket; |
| 183 |
| 184 | // Signalled each time a packet is sent on this channel. |
| 185 | sigslot::signal2<PacketTransportInternal*, const rtc::SentPacket&> |
| 186 | SignalSentPacket; |
| 187 |
| 188 | // Signalled when the current network route has changed. |
| 189 | sigslot::signal1<absl::optional<rtc::NetworkRoute>> SignalNetworkRouteChanged; |
| 190 |
| 191 | // Signalled when the transport is closed. |
| 192 | sigslot::signal1<PacketTransportInternal*> SignalClosed;*/ |
| 193 |
| 194 | private: |
| 195 | void runStateTimer() { |
| 196 | const auto weakSelf = std::weak_ptr<DirectPacketTransport>(shared_from_this()); |
| 197 | _thread->PostDelayedTask([weakSelf]() { |
| 198 | auto strongSelf = weakSelf.lock(); |
| 199 | if (!strongSelf) { |
| 200 | return; |
| 201 | } |
| 202 |
| 203 | strongSelf->updateState(); |
| 204 | strongSelf->runStateTimer(); |
| 205 | }, webrtc::TimeDelta::Millis(100)); |
| 206 | } |
| 207 |
| 208 | std::unique_ptr<std::vector<uint8_t>> makeServerHelloPacket() { |
| 209 | rtc::ByteBufferWriter bufferWriter; |
| 210 | for (int i = 0; i < 12; i++) { |
| 211 | bufferWriter.WriteUInt8(0xffu); |
| 212 | } |
| 213 | bufferWriter.WriteUInt8(0xfeu); |
| 214 | for (int i = 0; i < 3; i++) { |
| 215 | bufferWriter.WriteUInt8(0xffu); |
| 216 | } |
| 217 | bufferWriter.WriteUInt64(123); |
| 218 |
| 219 | while (bufferWriter.Length() % 4 != 0) { |
| 220 | bufferWriter.WriteUInt8(0); |
| 221 | } |
| 222 |
| 223 | return std::make_unique<std::vector<uint8_t>>(bufferWriter.Data(), bufferWriter.Data() + bufferWriter.Length()); |
| 224 | } |
| 225 |
| 226 | std::unique_ptr<std::vector<uint8_t>> makePingPacket() { |
| 227 | rtc::ByteBufferWriter bufferWriter; |
| 228 | bufferWriter.WriteUInt32(_pingMarker); |
| 229 |
| 230 | return std::make_unique<std::vector<uint8_t>>(bufferWriter.Data(), bufferWriter.Data() + bufferWriter.Length()); |
| 231 | } |
| 232 |
| 233 | void updateState() { |
| 234 | auto timestamp = rtc::TimeMillis(); |
| 235 |
| 236 | if (_isConnected && _lastDataReceivedTimestamp < timestamp - _keepalivePingInterval * 2) { |
| 237 | _isConnected = false; |
| 238 | SignalWritableState(this); |
| 239 |
| 240 | if (_isConnectedUpdated) { |
| 241 | _isConnectedUpdated(_isConnected); |
| 242 | } |
| 243 | } |
| 244 |
| 245 | if (_isConnected) { |
| 246 | if (_lastPingSentTimestamp < timestamp - _initialPingInterval) { |
| 247 | _lastPingSentTimestamp = timestamp; |
| 248 |
| 249 | auto packet = makePingPacket(); |
| 250 | _channel->sendPacket(std::move(packet)); |
| 251 | } |
| 252 | } else { |
| 253 | if (_lastPingSentTimestamp < timestamp - _keepalivePingInterval) { |
| 254 | _lastPingSentTimestamp = timestamp; |
| 255 |
| 256 | if (_hasCompletedHello) { |
| 257 | auto packet = makePingPacket(); |
| 258 | _channel->sendPacket(std::move(packet)); |
| 259 | } else { |
| 260 | auto packet = makeServerHelloPacket(); |
| 261 | _channel->sendPacket(std::move(packet)); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 |
| 267 | void processIncomingPacket(std::shared_ptr<std::vector<uint8_t>> const &packet) { |
| 268 | rtc::ByteBufferReader reader(rtc::ArrayView<const uint8_t>(reinterpret_cast<const uint8_t *>(packet->data()), packet->size())); |
| 269 |
| 270 | uint32_t header = 0; |
| 271 | if (!reader.ReadUInt32(&header)) { |
| 272 | return; |
| 273 | } |
| 274 |
| 275 | _lastDataReceivedTimestamp = rtc::TimeMillis(); |
| 276 | if (!_isConnected) { |
| 277 | _isConnected = true; |
| 278 | SignalWritableState(this); |
| 279 | SignalReadyToSend(this); |
| 280 | SignalReceivingState(this); |
| 281 |
| 282 | if (_isConnectedUpdated) { |
| 283 | _isConnectedUpdated(_isConnected); |
| 284 | } |
| 285 | } |
| 286 |
| 287 | if (header == _pingMarker) { |
| 288 | } else { |
| 289 | bool isSpecialPacket = false; |
| 290 | if (packet->size() >= 12) { |
| 291 | uint8_t specialTag[12]; |
| 292 | memcpy(specialTag, packet->data(), 12); |
| 293 |
| 294 | uint8_t expectedSpecialTag[12]; |
| 295 | memset(expectedSpecialTag, 0xff, 12); |
| 296 |
| 297 | if (memcmp(specialTag, expectedSpecialTag, 12) == 0) { |
| 298 | isSpecialPacket = true; |
| 299 | } |
| 300 | } |
| 301 |
| 302 | if (!isSpecialPacket) { |
| 303 | rtc::ByteBufferReader dataPacketReader(rtc::ArrayView<const uint8_t>(reinterpret_cast<const uint8_t *>(packet->data()), packet->size())); |
| 304 | uint32_t dataSize = 0; |
| 305 | if (!dataPacketReader.ReadUInt32(&dataSize)) { |
| 306 | return; |
| 307 | } |
| 308 | if (dataSize > packet->size() - 4) { |
| 309 | return; |
| 310 | } |
| 311 |
| 312 | if (auto decrypted = _encryption.handleIncomingPacket(reinterpret_cast<const char *>(packet->data()) + 4, dataSize)) { |
| 313 | handleIncomingMessage(decrypted->main); |
| 314 | for (auto &message : decrypted->additional) { |
| 315 | handleIncomingMessage(message); |
| 316 | } |
| 317 |
| 318 | /*if (_transportMessageReceived) { |
| 319 | _transportMessageReceived(std::move(decrypted->main)); |
| 320 | for (auto &message : decrypted->additional) { |
| 321 | _transportMessageReceived(std::move(message)); |
| 322 | } |
| 323 | }*/ |
| 324 | } else { |
| 325 | RTC_LOG(LS_ERROR) << "DirectPacketTransport: could not decrypt incoming packet"; |
| 326 | } |
| 327 |
| 328 | /*uint32_t dataSize = 0; |
| 329 | memcpy(&dataSize, packet->data(), 4); |
| 330 | dataSize = be32toh(dataSize); |
| 331 | if (dataSize > packet->size() - 4) { |
| 332 | RTC_LOG(LS_WARNING) << "DirectPacketTransport: Received data packet with invalid size tag"; |
| 333 | } else { |
| 334 | SignalReadPacket(this, reinterpret_cast<const char *>(packet->data() + 4), dataSize, rtc::TimeMicros(), 0); |
| 335 | }*/ |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 |
| 340 | void handleIncomingMessage(DecryptedMessage const &message) { |
| 341 | const auto data = &message.message.data; |
| 342 | if (const auto dataMessage = absl::get_if<AudioDataMessage>(data)) { |
| 343 | SignalReadPacket(this, reinterpret_cast<const char *>(dataMessage->data.data()), dataMessage->data.size(), rtc::TimeMicros(), 0); |
| 344 | } else if (const auto dataMessage = absl::get_if<VideoDataMessage>(data)) { |
| 345 | SignalReadPacket(this, reinterpret_cast<const char *>(dataMessage->data.data()), dataMessage->data.size(), rtc::TimeMicros(), 1); |
| 346 | } else { |
| 347 | RTC_LOG(LS_INFO) << "DirectPacketTransport: unknown incoming message"; |
| 348 | } |
| 349 | } |
| 350 |
| 351 | private: |
| 352 | std::string name = "DirectPacketTransport"; |
| 353 |
| 354 | std::function<void(bool)> _isConnectedUpdated; |
| 355 |
| 356 | rtc::Thread *_thread = nullptr; |
| 357 | EncryptedConnection _encryption; |
| 358 | std::shared_ptr<DirectConnectionChannel> _channel; |
| 359 |
| 360 | std::vector<uint8_t> _onIncomingPacketToken; |
| 361 |
| 362 | int _lastError = 0; |
| 363 |
| 364 | int64_t _lastPingSentTimestamp = 0; |
| 365 | int64_t _lastDataReceivedTimestamp = 0; |
| 366 | bool _isConnected = false; |
| 367 |
| 368 | bool _hasCompletedHello = false; |
| 369 |
| 370 | uint32_t _pingMarker = 0xabcd0102; |
| 371 | int64_t _initialPingInterval = 100; |
| 372 | int64_t _keepalivePingInterval = 1000; |
| 373 | }; |
| 374 |
| 375 | class DirectRtpTransport : public webrtc::RtpTransport { |
| 376 | public: |
| 377 | explicit DirectRtpTransport() : |
| 378 | webrtc::RtpTransport(true) { |
| 379 | } |
| 380 |
| 381 | virtual bool IsSrtpActive() const override { |
| 382 | return true; |
| 383 | } |
| 384 | }; |
| 385 |
| 386 | DirectNetworkingImpl::DirectNetworkingImpl(Configuration &&configuration) : |
| 387 | _threads(std::move(configuration.threads)), |
| 388 | _isOutgoing(configuration.isOutgoing), |
| 389 | _rtcServers(configuration.rtcServers), |
| 390 | _stateUpdated(std::move(configuration.stateUpdated)), |
| 391 | _transportMessageReceived(std::move(configuration.transportMessageReceived)), |
| 392 | _rtcpPacketReceived(std::move(configuration.rtcpPacketReceived)), |
| 393 | _dataChannelStateUpdated(configuration.dataChannelStateUpdated), |
| 394 | _dataChannelMessageReceived(configuration.dataChannelMessageReceived) { |
| 395 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 396 |
| 397 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), true); |
| 398 |
| 399 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 400 |
| 401 | _rtpTransport = std::make_unique<DirectRtpTransport>(); |
| 402 |
| 403 | _rtpTransport->SubscribeReadyToSend(this, [this](bool value) { |
| 404 | this->DtlsReadyToSend(value); |
| 405 | }); |
| 406 | _rtpTransport->SubscribeRtcpPacketReceived(this, [this](rtc::CopyOnWriteBuffer *packet, int64_t timestamp) { |
| 407 | this->OnRtcpPacketReceived_n(packet, timestamp); |
| 408 | }); |
| 409 |
| 410 | _directConnectionChannel = configuration.directConnectionChannel; |
| 411 | _packetTransport = std::make_shared<DirectPacketTransport>( |
| 412 | _threads->getNetworkThread(), |
| 413 | configuration.encryptionKey, |
| 414 | _directConnectionChannel, |
| 415 | [this](bool isConnected) { |
| 416 | this->_transportIsConnected = isConnected; |
| 417 | this->UpdateAggregateStates_n(); |
| 418 | } |
| 419 | ); |
| 420 |
| 421 | resetDtlsSrtpTransport(); |
| 422 | } |
| 423 |
| 424 | DirectNetworkingImpl::~DirectNetworkingImpl() { |
| 425 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 426 |
| 427 | RTC_LOG(LS_INFO) << "DirectNetworkingImpl::~DirectNetworkingImpl()"; |
| 428 |
| 429 | _rtpTransport.reset(); |
| 430 | _dataChannelInterface.reset(); |
| 431 | _packetTransport.reset(); |
| 432 | } |
| 433 |
| 434 | void DirectNetworkingImpl::resetDtlsSrtpTransport() { |
| 435 | _rtpTransport->SetRtpPacketTransport(_packetTransport.get()); |
| 436 | } |
| 437 |
| 438 | void DirectNetworkingImpl::start() { |
| 439 | const auto weak = std::weak_ptr<DirectNetworkingImpl>(shared_from_this()); |
| 440 | _dataChannelInterface.reset(new SctpDataChannelProviderInterfaceImpl( |
| 441 | _packetTransport.get(), |
| 442 | _isOutgoing, |
| 443 | [weak, threads = _threads](bool state) { |
| 444 | assert(threads->getNetworkThread()->IsCurrent()); |
| 445 | const auto strong = weak.lock(); |
| 446 | if (!strong) { |
| 447 | return; |
| 448 | } |
| 449 | strong->_dataChannelStateUpdated(state); |
| 450 | }, |
| 451 | [weak, threads = _threads]() { |
| 452 | assert(threads->getNetworkThread()->IsCurrent()); |
| 453 | const auto strong = weak.lock(); |
| 454 | if (!strong) { |
| 455 | return; |
| 456 | } |
| 457 | //strong->restartDataChannel(); |
| 458 | }, |
| 459 | [weak, threads = _threads](std::string const &message) { |
| 460 | assert(threads->getNetworkThread()->IsCurrent()); |
| 461 | const auto strong = weak.lock(); |
| 462 | if (!strong) { |
| 463 | return; |
| 464 | } |
| 465 | strong->_dataChannelMessageReceived(message); |
| 466 | }, |
| 467 | _threads |
| 468 | )); |
| 469 |
| 470 | _lastDisconnectedTimestamp = rtc::TimeMillis(); |
| 471 | checkConnectionTimeout(); |
| 472 |
| 473 | _packetTransport->start(); |
| 474 | } |
| 475 |
| 476 | void DirectNetworkingImpl::stop() { |
| 477 | _rtpTransport->UnsubscribeReadyToSend(this); |
| 478 |
| 479 | _dataChannelInterface.reset(); |
| 480 | _rtpTransport.reset(); |
| 481 |
| 482 | _packetTransport->stop(); |
| 483 |
| 484 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), true); |
| 485 |
| 486 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 487 | } |
| 488 |
| 489 | PeerIceParameters DirectNetworkingImpl::getLocalIceParameters() { |
| 490 | return _localIceParameters; |
| 491 | } |
| 492 |
| 493 | std::unique_ptr<rtc::SSLFingerprint> DirectNetworkingImpl::getLocalFingerprint() { |
| 494 | auto certificate = _localCertificate; |
| 495 | if (!certificate) { |
| 496 | return nullptr; |
| 497 | } |
| 498 | return rtc::SSLFingerprint::CreateFromCertificate(*certificate); |
| 499 | } |
| 500 |
| 501 | void DirectNetworkingImpl::setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup) { |
| 502 | } |
| 503 |
| 504 | void DirectNetworkingImpl::addCandidates(std::vector<cricket::Candidate> const &candidates) { |
| 505 | } |
| 506 |
| 507 | void DirectNetworkingImpl::sendDataChannelMessage(std::string const &message) { |
| 508 | if (_dataChannelInterface) { |
| 509 | _dataChannelInterface->sendDataChannelMessage(message); |
| 510 | } |
| 511 | } |
| 512 |
| 513 | webrtc::RtpTransport *DirectNetworkingImpl::getRtpTransport() { |
| 514 | return _rtpTransport.get(); |
| 515 | } |
| 516 |
| 517 | void DirectNetworkingImpl::checkConnectionTimeout() { |
| 518 | const auto weak = std::weak_ptr<DirectNetworkingImpl>(shared_from_this()); |
| 519 | _threads->getNetworkThread()->PostDelayedTask([weak]() { |
| 520 | auto strong = weak.lock(); |
| 521 | if (!strong) { |
| 522 | return; |
| 523 | } |
| 524 |
| 525 | int64_t currentTimestamp = rtc::TimeMillis(); |
| 526 | const int64_t maxTimeout = 20000; |
| 527 |
| 528 | if (!strong->_isConnected && strong->_lastDisconnectedTimestamp + maxTimeout < currentTimestamp) { |
| 529 | RTC_LOG(LS_INFO) << "DirectNetworkingImpl timeout " << (currentTimestamp - strong->_lastDisconnectedTimestamp) << " ms"; |
| 530 |
| 531 | strong->_isFailed = true; |
| 532 | strong->notifyStateUpdated(); |
| 533 | } |
| 534 |
| 535 | strong->checkConnectionTimeout(); |
| 536 | }, webrtc::TimeDelta::Millis(1000)); |
| 537 | } |
| 538 |
| 539 | void DirectNetworkingImpl::OnTransportWritableState_n(rtc::PacketTransportInternal *transport) { |
| 540 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 541 |
| 542 | UpdateAggregateStates_n(); |
| 543 | } |
| 544 | void DirectNetworkingImpl::OnTransportReceivingState_n(rtc::PacketTransportInternal *transport) { |
| 545 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 546 |
| 547 | UpdateAggregateStates_n(); |
| 548 | } |
| 549 |
| 550 | void DirectNetworkingImpl::DtlsReadyToSend(bool isReadyToSend) { |
| 551 | UpdateAggregateStates_n(); |
| 552 |
| 553 | if (isReadyToSend) { |
| 554 | const auto weak = std::weak_ptr<DirectNetworkingImpl>(shared_from_this()); |
| 555 | _threads->getNetworkThread()->PostTask([weak]() { |
| 556 | const auto strong = weak.lock(); |
| 557 | if (!strong) { |
| 558 | return; |
| 559 | } |
| 560 | strong->UpdateAggregateStates_n(); |
| 561 | }); |
| 562 | } |
| 563 | } |
| 564 |
| 565 | void DirectNetworkingImpl::OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us) { |
| 566 | if (_rtcpPacketReceived) { |
| 567 | _rtcpPacketReceived(*packet, packet_time_us); |
| 568 | } |
| 569 | } |
| 570 |
| 571 | void DirectNetworkingImpl::UpdateAggregateStates_n() { |
| 572 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 573 |
| 574 | bool isConnected = _transportIsConnected; |
| 575 |
| 576 | if (_isConnected != isConnected) { |
| 577 | _isConnected = isConnected; |
| 578 |
| 579 | if (!isConnected) { |
| 580 | _lastDisconnectedTimestamp = rtc::TimeMillis(); |
| 581 | } |
| 582 |
| 583 | notifyStateUpdated(); |
| 584 |
| 585 | if (_dataChannelInterface) { |
| 586 | _dataChannelInterface->updateIsConnected(isConnected); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 |
| 591 | void DirectNetworkingImpl::notifyStateUpdated() { |
| 592 | DirectNetworkingImpl::State emitState; |
| 593 | emitState.isReadyToSendData = _isConnected; |
| 594 | emitState.route = _currentRouteDescription; |
| 595 | emitState.connection = _currentConnectionDescription; |
| 596 | emitState.isFailed = _isFailed; |
| 597 | _stateUpdated(emitState); |
| 598 | } |
| 599 |
| 600 | } // namespace tgcalls |
| 601 |