| #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 | class CryptStringImpl : public rtc::CryptStringImpl { |
| 33 | public: |
| 34 | CryptStringImpl(std::string const &value) : |
| 35 | _value(value) { |
| 36 | } |
| 37 |
| 38 | virtual ~CryptStringImpl() override { |
| 39 | } |
| 40 |
| 41 | virtual size_t GetLength() const override { |
| 42 | return _value.size(); |
| 43 | } |
| 44 |
| 45 | virtual void CopyTo(char* dest, bool nullterminate) const override { |
| 46 | memcpy(dest, _value.data(), _value.size()); |
| 47 | if (nullterminate) { |
| 48 | dest[_value.size()] = 0; |
| 49 | } |
| 50 | } |
| 51 | virtual std::string UrlEncode() const override { |
| 52 | return _value; |
| 53 | } |
| 54 | virtual CryptStringImpl* Copy() const override { |
| 55 | return new CryptStringImpl(_value); |
| 56 | } |
| 57 |
| 58 | virtual void CopyRawTo(std::vector<unsigned char>* dest) const override { |
| 59 | dest->resize(_value.size()); |
| 60 | memcpy(dest->data(), _value.data(), _value.size()); |
| 61 | } |
| 62 |
| 63 | private: |
| 64 | std::string _value; |
| 65 | }; |
| 66 |
| 67 | class WrappedAsyncPacketSocket : public rtc::AsyncPacketSocket { |
| 68 | public: |
| 69 | WrappedAsyncPacketSocket(std::unique_ptr<rtc::AsyncPacketSocket> &&wrappedSocket) : |
| 70 | _wrappedSocket(std::move(wrappedSocket)) { |
| 71 | _wrappedSocket->SignalReadPacket.connect(this, &WrappedAsyncPacketSocket::onReadPacket); |
| 72 | _wrappedSocket->SignalSentPacket.connect(this, &WrappedAsyncPacketSocket::onSentPacket); |
| 73 | _wrappedSocket->SignalReadyToSend.connect(this, &WrappedAsyncPacketSocket::onReadyToSend); |
| 74 | _wrappedSocket->SignalAddressReady.connect(this, &WrappedAsyncPacketSocket::onAddressReady); |
| 75 | _wrappedSocket->SignalConnect.connect(this, &WrappedAsyncPacketSocket::onConnect); |
| 76 | _wrappedSocket->SubscribeClose(this, [this](AsyncPacketSocket* socket, int error) { onClose(socket, error); }); |
| 77 | } |
| 78 |
| 79 | virtual ~WrappedAsyncPacketSocket() override { |
| 80 | _wrappedSocket->SignalReadPacket.disconnect(this); |
| 81 | _wrappedSocket->SignalSentPacket.disconnect(this); |
| 82 | _wrappedSocket->SignalReadyToSend.disconnect(this); |
| 83 | _wrappedSocket->SignalAddressReady.disconnect(this); |
| 84 | _wrappedSocket->SignalConnect.disconnect(this); |
| 85 | _wrappedSocket->UnsubscribeClose(this); |
| 86 |
| 87 | _wrappedSocket.reset(); |
| 88 | } |
| 89 |
| 90 | virtual rtc::SocketAddress GetLocalAddress() const override { |
| 91 | return _wrappedSocket->GetLocalAddress(); |
| 92 | } |
| 93 |
| 94 | virtual rtc::SocketAddress GetRemoteAddress() const override { |
| 95 | return _wrappedSocket->GetRemoteAddress(); |
| 96 | } |
| 97 |
| 98 | virtual int Send(const void* pv, size_t cb, const rtc::PacketOptions& options) override { |
| 99 | return _wrappedSocket->Send(pv, cb, options); |
| 100 | } |
| 101 |
| 102 | virtual int SendTo(const void* pv, |
| 103 | size_t cb, |
| 104 | const rtc::SocketAddress& addr, |
| 105 | const rtc::PacketOptions& options) override { |
| 106 | return _wrappedSocket->SendTo(pv, cb, addr, options); |
| 107 | } |
| 108 |
| 109 | virtual int Close() override { |
| 110 | return _wrappedSocket->Close(); |
| 111 | } |
| 112 |
| 113 | virtual State GetState() const override { |
| 114 | return _wrappedSocket->GetState(); |
| 115 | } |
| 116 |
| 117 | virtual int GetOption(rtc::Socket::Option opt, int* value) override { |
| 118 | return _wrappedSocket->GetOption(opt, value); |
| 119 | } |
| 120 |
| 121 | virtual int SetOption(rtc::Socket::Option opt, int value) override { |
| 122 | return _wrappedSocket->SetOption(opt, value); |
| 123 | } |
| 124 |
| 125 | virtual int GetError() const override { |
| 126 | return _wrappedSocket->GetError(); |
| 127 | } |
| 128 |
| 129 | virtual void SetError(int error) override { |
| 130 | _wrappedSocket->SetError(error); |
| 131 | } |
| 132 |
| 133 | private: |
| 134 | void onReadPacket(AsyncPacketSocket *socket, const char *data, size_t size, const rtc::SocketAddress &address, const int64_t ×tamp) { |
| 135 | SignalReadPacket.emit(this, data, size, address, timestamp); |
| 136 | } |
| 137 |
| 138 | void onSentPacket(AsyncPacketSocket *socket, const rtc::SentPacket &packet) { |
| 139 | SignalSentPacket.emit(this, packet); |
| 140 | } |
| 141 |
| 142 | void onReadyToSend(AsyncPacketSocket *socket) { |
| 143 | SignalReadyToSend.emit(this); |
| 144 | } |
| 145 |
| 146 | void onAddressReady(AsyncPacketSocket *socket, const rtc::SocketAddress &address) { |
| 147 | SignalAddressReady.emit(this, address); |
| 148 | } |
| 149 |
| 150 | void onConnect(AsyncPacketSocket *socket) { |
| 151 | SignalConnect.emit(this); |
| 152 | } |
| 153 |
| 154 | void onClose(AsyncPacketSocket *socket, int value) { |
| 155 | SignalClose(this, value); |
| 156 | } |
| 157 |
| 158 | private: |
| 159 | std::unique_ptr<rtc::AsyncPacketSocket> _wrappedSocket; |
| 160 | }; |
| 161 |
| 162 | class WrappedBasicPacketSocketFactory : public rtc::BasicPacketSocketFactory { |
| 163 | public: |
| 164 | explicit WrappedBasicPacketSocketFactory(rtc::SocketFactory* socket_factory) : |
| 165 | rtc::BasicPacketSocketFactory(socket_factory) { |
| 166 | } |
| 167 |
| 168 | virtual ~WrappedBasicPacketSocketFactory() override { |
| 169 | } |
| 170 |
| 171 | rtc::AsyncPacketSocket* CreateUdpSocket(const rtc::SocketAddress& local_address, uint16_t min_port, uint16_t max_port) override { |
| 172 | rtc::AsyncPacketSocket *socket = rtc::BasicPacketSocketFactory::CreateUdpSocket(local_address, min_port, max_port); |
| 173 | if (socket) { |
| 174 | std::unique_ptr<rtc::AsyncPacketSocket> socketPtr; |
| 175 | socketPtr.reset(socket); |
| 176 |
| 177 | return new WrappedAsyncPacketSocket(std::move(socketPtr)); |
| 178 | } else { |
| 179 | return nullptr; |
| 180 | } |
| 181 | } |
| 182 |
| 183 | private: |
| 184 |
| 185 | }; |
| 186 |
| 187 | } |
| 188 |
| 189 | NativeNetworkingImpl::ConnectionDescription::CandidateDescription NativeNetworkingImpl::connectionDescriptionFromCandidate(cricket::Candidate const &candidate) { |
| 190 | NativeNetworkingImpl::ConnectionDescription::CandidateDescription result; |
| 191 |
| 192 | result.type = candidate.type(); |
| 193 | result.protocol = candidate.protocol(); |
| 194 | result.address = candidate.address().ToString(); |
| 195 |
| 196 | return result; |
| 197 | } |
| 198 |
| 199 | webrtc::CryptoOptions NativeNetworkingImpl::getDefaulCryptoOptions() { |
| 200 | auto options = webrtc::CryptoOptions(); |
| 201 | options.srtp.enable_aes128_sha1_80_crypto_cipher = true; |
| 202 | options.srtp.enable_gcm_crypto_suites = true; |
| 203 | return options; |
| 204 | } |
| 205 |
| 206 | NativeNetworkingImpl::NativeNetworkingImpl(Configuration &&configuration) : |
| 207 | _threads(std::move(configuration.threads)), |
| 208 | _isOutgoing(configuration.isOutgoing), |
| 209 | _enableStunMarking(configuration.enableStunMarking), |
| 210 | _enableTCP(configuration.enableTCP), |
| 211 | _enableP2P(configuration.enableP2P), |
| 212 | _rtcServers(configuration.rtcServers), |
| 213 | _proxy(configuration.proxy), |
| 214 | _stateUpdated(std::move(configuration.stateUpdated)), |
| 215 | _candidateGathered(std::move(configuration.candidateGathered)), |
| 216 | _transportMessageReceived(std::move(configuration.transportMessageReceived)), |
| 217 | _rtcpPacketReceived(std::move(configuration.rtcpPacketReceived)), |
| 218 | _dataChannelStateUpdated(configuration.dataChannelStateUpdated), |
| 219 | _dataChannelMessageReceived(configuration.dataChannelMessageReceived) { |
| 220 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 221 |
| 222 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), true); |
| 223 |
| 224 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 225 |
| 226 | _networkMonitorFactory = PlatformInterface::SharedInstance()->createNetworkMonitorFactory(); |
| 227 | _socketFactory.reset(new rtc::BasicPacketSocketFactory(_threads->getNetworkThread()->socketserver())); |
| 228 | _networkManager = std::make_unique<rtc::BasicNetworkManager>(_networkMonitorFactory.get(), _threads->getNetworkThread()->socketserver()); |
| 229 |
| 230 | _asyncResolverFactory = std::make_unique<webrtc::WrappingAsyncDnsResolverFactory>(std::make_unique<webrtc::BasicAsyncResolverFactory>()); |
| 231 |
| 232 | _dtlsSrtpTransport = std::make_unique<webrtc::DtlsSrtpTransport>(true, fieldTrialsBasedConfig); |
| 233 | _dtlsSrtpTransport->SetDtlsTransports(nullptr, nullptr); |
| 234 | _dtlsSrtpTransport->SetActiveResetSrtpParams(false); |
| 235 | _dtlsSrtpTransport->SignalReadyToSend.connect(this, &NativeNetworkingImpl::DtlsReadyToSend); |
| 236 | //_dtlsSrtpTransport->SignalRtpPacketReceived.connect(this, &NativeNetworkingImpl::RtpPacketReceived_n); |
| 237 | _dtlsSrtpTransport->SignalRtcpPacketReceived.connect(this, &NativeNetworkingImpl::OnRtcpPacketReceived_n); |
| 238 |
| 239 | resetDtlsSrtpTransport(); |
| 240 | } |
| 241 |
| 242 | NativeNetworkingImpl::~NativeNetworkingImpl() { |
| 243 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 244 |
| 245 | RTC_LOG(LS_INFO) << "NativeNetworkingImpl::~NativeNetworkingImpl()"; |
| 246 |
| 247 | _dtlsSrtpTransport.reset(); |
| 248 | _dtlsTransport.reset(); |
| 249 | _dataChannelInterface.reset(); |
| 250 | _transportChannel.reset(); |
| 251 | _asyncResolverFactory.reset(); |
| 252 | _portAllocator.reset(); |
| 253 | _networkManager.reset(); |
| 254 | _socketFactory.reset(); |
| 255 | _networkMonitorFactory.reset(); |
| 256 | } |
| 257 |
| 258 | void NativeNetworkingImpl::resetDtlsSrtpTransport() { |
| 259 | if (_enableStunMarking) { |
| 260 | _turnCustomizer.reset(new TurnCustomizerImpl()); |
| 261 | } |
| 262 |
| 263 | _relayPortFactory.reset(new ReflectorRelayPortFactory(_rtcServers)); |
| 264 |
| 265 | _portAllocator.reset(new cricket::BasicPortAllocator(_networkManager.get(), _socketFactory.get(), _turnCustomizer.get(), _relayPortFactory.get())); |
| 266 |
| 267 | uint32_t flags = _portAllocator->flags(); |
| 268 |
| 269 | flags |= |
| 270 | //cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 271 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 272 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
| 273 |
| 274 | /*if (_proxy) { |
| 275 | rtc::ProxyInfo proxyInfo; |
| 276 | proxyInfo.type = rtc::ProxyType::PROXY_SOCKS5; |
| 277 | proxyInfo.address = rtc::SocketAddress(_proxy->host, _proxy->port); |
| 278 | proxyInfo.username = _proxy->login; |
| 279 | proxyInfo.password = rtc::CryptString(CryptStringImpl(_proxy->password)); |
| 280 | _portAllocator->set_proxy("t/1.0", proxyInfo); |
| 281 |
| 282 | flags &= ~cricket::PORTALLOCATOR_DISABLE_TCP; |
| 283 | } else */{ |
| 284 | if (!_enableTCP) { |
| 285 | flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 286 | } |
| 287 | } |
| 288 |
| 289 | if (_proxy || !_enableP2P) { |
| 290 | flags |= cricket::PORTALLOCATOR_DISABLE_UDP; |
| 291 | flags |= cricket::PORTALLOCATOR_DISABLE_STUN; |
| 292 | uint32_t candidateFilter = _portAllocator->candidate_filter(); |
| 293 | candidateFilter &= ~(cricket::CF_REFLEXIVE); |
| 294 | _portAllocator->SetCandidateFilter(candidateFilter); |
| 295 | } |
| 296 |
| 297 | _portAllocator->set_step_delay(cricket::kMinimumStepDelay); |
| 298 |
| 299 | _portAllocator->set_flags(flags); |
| 300 | _portAllocator->Initialize(); |
| 301 |
| 302 | cricket::ServerAddresses stunServers; |
| 303 | std::vector<cricket::RelayServerConfig> turnServers; |
| 304 |
| 305 | for (auto &server : _rtcServers) { |
| 306 | if (server.isTcp) { |
| 307 | continue; |
| 308 | } |
| 309 |
| 310 | if (server.isTurn) { |
| 311 | turnServers.push_back(cricket::RelayServerConfig( |
| 312 | rtc::SocketAddress(server.host, server.port), |
| 313 | server.login, |
| 314 | server.password, |
| 315 | cricket::PROTO_UDP |
| 316 | )); |
| 317 | } else { |
| 318 | rtc::SocketAddress stunAddress = rtc::SocketAddress(server.host, server.port); |
| 319 | stunServers.insert(stunAddress); |
| 320 | } |
| 321 | } |
| 322 |
| 323 | _portAllocator->SetConfiguration(stunServers, turnServers, 0, webrtc::NO_PRUNE, _turnCustomizer.get()); |
| 324 |
| 325 | webrtc::IceTransportInit iceTransportInit; |
| 326 | iceTransportInit.set_port_allocator(_portAllocator.get()); |
| 327 | iceTransportInit.set_async_dns_resolver_factory(_asyncResolverFactory.get()); |
| 328 |
| 329 | _transportChannel = cricket::P2PTransportChannel::Create("transport", 0, std::move(iceTransportInit)); |
| 330 |
| 331 | cricket::IceConfig iceConfig; |
| 332 | iceConfig.continual_gathering_policy = cricket::GATHER_CONTINUALLY; |
| 333 | iceConfig.prioritize_most_likely_candidate_pairs = true; |
| 334 | iceConfig.regather_on_failed_networks_interval = 8000; |
| 335 | _transportChannel->SetIceConfig(iceConfig); |
| 336 |
| 337 | cricket::IceParameters localIceParameters( |
| 338 | _localIceParameters.ufrag, |
| 339 | _localIceParameters.pwd, |
| 340 | _localIceParameters.supportsRenomination |
| 341 | ); |
| 342 |
| 343 | _transportChannel->SetIceParameters(localIceParameters); |
| 344 | _transportChannel->SetIceRole(_isOutgoing ? cricket::ICEROLE_CONTROLLING : cricket::ICEROLE_CONTROLLED); |
| 345 | _transportChannel->SetRemoteIceMode(cricket::ICEMODE_FULL); |
| 346 |
| 347 | _transportChannel->SignalCandidateGathered.connect(this, &NativeNetworkingImpl::candidateGathered); |
| 348 | _transportChannel->SignalIceTransportStateChanged.connect(this, &NativeNetworkingImpl::transportStateChanged); |
| 349 | _transportChannel->SignalCandidatePairChanged.connect(this, &NativeNetworkingImpl::candidatePairChanged); |
| 350 | _transportChannel->SignalNetworkRouteChanged.connect(this, &NativeNetworkingImpl::transportRouteChanged); |
| 351 |
| 352 | webrtc::CryptoOptions cryptoOptions = NativeNetworkingImpl::getDefaulCryptoOptions(); |
| 353 | _dtlsTransport.reset(new cricket::DtlsTransport(_transportChannel.get(), cryptoOptions, nullptr)); |
| 354 |
| 355 | _dtlsTransport->SignalWritableState.connect( |
| 356 | this, &NativeNetworkingImpl::OnTransportWritableState_n); |
| 357 | _dtlsTransport->SignalReceivingState.connect( |
| 358 | this, &NativeNetworkingImpl::OnTransportReceivingState_n); |
| 359 |
| 360 | _dtlsTransport->SetLocalCertificate(_localCertificate); |
| 361 |
| 362 | _dtlsSrtpTransport->SetDtlsTransports(_dtlsTransport.get(), nullptr); |
| 363 | } |
| 364 |
| 365 | void NativeNetworkingImpl::start() { |
| 366 | _transportChannel->MaybeStartGathering(); |
| 367 |
| 368 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 369 | _dataChannelInterface.reset(new SctpDataChannelProviderInterfaceImpl( |
| 370 | _dtlsTransport.get(), |
| 371 | _isOutgoing, |
| 372 | [weak, threads = _threads](bool state) { |
| 373 | assert(threads->getNetworkThread()->IsCurrent()); |
| 374 | const auto strong = weak.lock(); |
| 375 | if (!strong) { |
| 376 | return; |
| 377 | } |
| 378 | strong->_dataChannelStateUpdated(state); |
| 379 | }, |
| 380 | [weak, threads = _threads]() { |
| 381 | assert(threads->getNetworkThread()->IsCurrent()); |
| 382 | const auto strong = weak.lock(); |
| 383 | if (!strong) { |
| 384 | return; |
| 385 | } |
| 386 | //strong->restartDataChannel(); |
| 387 | }, |
| 388 | [weak, threads = _threads](std::string const &message) { |
| 389 | assert(threads->getNetworkThread()->IsCurrent()); |
| 390 | const auto strong = weak.lock(); |
| 391 | if (!strong) { |
| 392 | return; |
| 393 | } |
| 394 | strong->_dataChannelMessageReceived(message); |
| 395 | }, |
| 396 | _threads |
| 397 | )); |
| 398 |
| 399 | _lastDisconnectedTimestamp = rtc::TimeMillis(); |
| 400 | checkConnectionTimeout(); |
| 401 | } |
| 402 |
| 403 | void NativeNetworkingImpl::stop() { |
| 404 | _transportChannel->SignalCandidateGathered.disconnect(this); |
| 405 | _transportChannel->SignalIceTransportStateChanged.disconnect(this); |
| 406 | _transportChannel->SignalReadPacket.disconnect(this); |
| 407 | _transportChannel->SignalNetworkRouteChanged.disconnect(this); |
| 408 |
| 409 | _dtlsTransport->SignalWritableState.disconnect(this); |
| 410 | _dtlsTransport->SignalReceivingState.disconnect(this); |
| 411 |
| 412 | _dtlsSrtpTransport->SetDtlsTransports(nullptr, nullptr); |
| 413 |
| 414 | _dataChannelInterface.reset(); |
| 415 | _dtlsTransport.reset(); |
| 416 | _transportChannel.reset(); |
| 417 | _portAllocator.reset(); |
| 418 |
| 419 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH), true); |
| 420 |
| 421 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 422 | } |
| 423 |
| 424 | PeerIceParameters NativeNetworkingImpl::getLocalIceParameters() { |
| 425 | return _localIceParameters; |
| 426 | } |
| 427 |
| 428 | std::unique_ptr<rtc::SSLFingerprint> NativeNetworkingImpl::getLocalFingerprint() { |
| 429 | auto certificate = _localCertificate; |
| 430 | if (!certificate) { |
| 431 | return nullptr; |
| 432 | } |
| 433 | return rtc::SSLFingerprint::CreateFromCertificate(*certificate); |
| 434 | } |
| 435 |
| 436 | void NativeNetworkingImpl::setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup) { |
| 437 | _remoteIceParameters = remoteIceParameters; |
| 438 |
| 439 | cricket::IceParameters parameters( |
| 440 | remoteIceParameters.ufrag, |
| 441 | remoteIceParameters.pwd, |
| 442 | remoteIceParameters.supportsRenomination |
| 443 | ); |
| 444 |
| 445 | _transportChannel->SetRemoteIceParameters(parameters); |
| 446 |
| 447 | if (sslSetup == "active") { |
| 448 | _dtlsTransport->SetDtlsRole(rtc::SSLRole::SSL_SERVER); |
| 449 | } else if (sslSetup == "passive") { |
| 450 | _dtlsTransport->SetDtlsRole(rtc::SSLRole::SSL_CLIENT); |
| 451 | } else { |
| 452 | _dtlsTransport->SetDtlsRole(_isOutgoing ? rtc::SSLRole::SSL_CLIENT : rtc::SSLRole::SSL_SERVER); |
| 453 | } |
| 454 |
| 455 | if (fingerprint) { |
| 456 | _dtlsTransport->SetRemoteFingerprint(fingerprint->algorithm, fingerprint->digest.data(), fingerprint->digest.size()); |
| 457 | } |
| 458 | } |
| 459 |
| 460 | void NativeNetworkingImpl::addCandidates(std::vector<cricket::Candidate> const &candidates) { |
| 461 | for (const auto &candidate : candidates) { |
| 462 | _transportChannel->AddRemoteCandidate(candidate); |
| 463 | } |
| 464 | } |
| 465 |
| 466 | void NativeNetworkingImpl::sendDataChannelMessage(std::string const &message) { |
| 467 | if (_dataChannelInterface) { |
| 468 | _dataChannelInterface->sendDataChannelMessage(message); |
| 469 | } |
| 470 | } |
| 471 |
| 472 | webrtc::RtpTransport *NativeNetworkingImpl::getRtpTransport() { |
| 473 | return _dtlsSrtpTransport.get(); |
| 474 | } |
| 475 |
| 476 | void NativeNetworkingImpl::checkConnectionTimeout() { |
| 477 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 478 | _threads->getNetworkThread()->PostDelayedTask([weak]() { |
| 479 | auto strong = weak.lock(); |
| 480 | if (!strong) { |
| 481 | return; |
| 482 | } |
| 483 |
| 484 | int64_t currentTimestamp = rtc::TimeMillis(); |
| 485 | const int64_t maxTimeout = 20000; |
| 486 |
| 487 | if (!strong->_isConnected && strong->_lastDisconnectedTimestamp + maxTimeout < currentTimestamp) { |
| 488 | RTC_LOG(LS_INFO) << "NativeNetworkingImpl timeout " << (currentTimestamp - strong->_lastDisconnectedTimestamp) << " ms"; |
| 489 |
| 490 | strong->_isFailed = true; |
| 491 | strong->notifyStateUpdated(); |
| 492 | } |
| 493 |
| 494 | strong->checkConnectionTimeout(); |
| 495 | }, webrtc::TimeDelta::Millis(1000)); |
| 496 | } |
| 497 |
| 498 | void NativeNetworkingImpl::candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate) { |
| 499 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 500 |
| 501 | _candidateGathered(candidate); |
| 502 | } |
| 503 |
| 504 | void NativeNetworkingImpl::candidateGatheringState(cricket::IceTransportInternal *transport) { |
| 505 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 506 | } |
| 507 |
| 508 | void NativeNetworkingImpl::OnTransportWritableState_n(rtc::PacketTransportInternal *transport) { |
| 509 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 510 |
| 511 | UpdateAggregateStates_n(); |
| 512 | } |
| 513 | void NativeNetworkingImpl::OnTransportReceivingState_n(rtc::PacketTransportInternal *transport) { |
| 514 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 515 |
| 516 | UpdateAggregateStates_n(); |
| 517 | } |
| 518 |
| 519 | void NativeNetworkingImpl::DtlsReadyToSend(bool isReadyToSend) { |
| 520 | UpdateAggregateStates_n(); |
| 521 |
| 522 | if (isReadyToSend) { |
| 523 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 524 | _threads->getNetworkThread()->PostTask([weak]() { |
| 525 | const auto strong = weak.lock(); |
| 526 | if (!strong) { |
| 527 | return; |
| 528 | } |
| 529 | strong->UpdateAggregateStates_n(); |
| 530 | }); |
| 531 | } |
| 532 | } |
| 533 |
| 534 | void NativeNetworkingImpl::transportStateChanged(cricket::IceTransportInternal *transport) { |
| 535 | UpdateAggregateStates_n(); |
| 536 | } |
| 537 |
| 538 | void NativeNetworkingImpl::transportReadyToSend(cricket::IceTransportInternal *transport) { |
| 539 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 540 | } |
| 541 |
| 542 | void NativeNetworkingImpl::transportRouteChanged(absl::optional<rtc::NetworkRoute> route) { |
| 543 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 544 |
| 545 | if (route.has_value()) { |
| 546 | /*cricket::IceTransportStats iceTransportStats; |
| 547 | if (_transportChannel->GetStats(&iceTransportStats)) { |
| 548 | }*/ |
| 549 |
| 550 | RTC_LOG(LS_INFO) << "NativeNetworkingImpl route changed: " << route->DebugString(); |
| 551 |
| 552 | bool localIsWifi = route->local.adapter_type() == rtc::AdapterType::ADAPTER_TYPE_WIFI; |
| 553 | bool remoteIsWifi = route->remote.adapter_type() == rtc::AdapterType::ADAPTER_TYPE_WIFI; |
| 554 |
| 555 | RTC_LOG(LS_INFO) << "NativeNetworkingImpl is wifi: local=" << localIsWifi << ", remote=" << remoteIsWifi; |
| 556 |
| 557 | std::string localDescription = route->local.uses_turn() ? "turn" : "p2p"; |
| 558 | std::string remoteDescription = route->remote.uses_turn() ? "turn" : "p2p"; |
| 559 |
| 560 | RouteDescription routeDescription(localDescription, remoteDescription); |
| 561 |
| 562 | if (!_currentRouteDescription || routeDescription != _currentRouteDescription.value()) { |
| 563 | _currentRouteDescription = std::move(routeDescription); |
| 564 | notifyStateUpdated(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 |
| 569 | void NativeNetworkingImpl::candidatePairChanged(cricket::CandidatePairChangeEvent const &event) { |
| 570 | ConnectionDescription connectionDescription; |
| 571 |
| 572 | connectionDescription.local = connectionDescriptionFromCandidate(event.selected_candidate_pair.local); |
| 573 | connectionDescription.remote = connectionDescriptionFromCandidate(event.selected_candidate_pair.remote); |
| 574 |
| 575 | if (!_currentConnectionDescription || _currentConnectionDescription.value() != connectionDescription) { |
| 576 | _currentConnectionDescription = std::move(connectionDescription); |
| 577 | notifyStateUpdated(); |
| 578 | } |
| 579 | } |
| 580 |
| 581 | void NativeNetworkingImpl::RtpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us, bool isUnresolved) { |
| 582 | if (_transportMessageReceived) { |
| 583 | _transportMessageReceived(*packet, isUnresolved); |
| 584 | } |
| 585 | } |
| 586 |
| 587 | void NativeNetworkingImpl::OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us) { |
| 588 | if (_rtcpPacketReceived) { |
| 589 | _rtcpPacketReceived(*packet, packet_time_us); |
| 590 | } |
| 591 | } |
| 592 |
| 593 | void NativeNetworkingImpl::UpdateAggregateStates_n() { |
| 594 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 595 |
| 596 | auto state = _transportChannel->GetIceTransportState(); |
| 597 | bool isConnected = false; |
| 598 | switch (state) { |
| 599 | case webrtc::IceTransportState::kConnected: |
| 600 | case webrtc::IceTransportState::kCompleted: |
| 601 | isConnected = true; |
| 602 | break; |
| 603 | default: |
| 604 | break; |
| 605 | } |
| 606 |
| 607 | if (!_dtlsSrtpTransport->IsWritable(false)) { |
| 608 | isConnected = false; |
| 609 | } |
| 610 |
| 611 | if (_isConnected != isConnected) { |
| 612 | _isConnected = isConnected; |
| 613 |
| 614 | if (!isConnected) { |
| 615 | _lastDisconnectedTimestamp = rtc::TimeMillis(); |
| 616 | } |
| 617 |
| 618 | notifyStateUpdated(); |
| 619 |
| 620 | if (_dataChannelInterface) { |
| 621 | _dataChannelInterface->updateIsConnected(isConnected); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 |
| 626 | void NativeNetworkingImpl::notifyStateUpdated() { |
| 627 | NativeNetworkingImpl::State emitState; |
| 628 | emitState.isReadyToSendData = _isConnected; |
| 629 | emitState.route = _currentRouteDescription; |
| 630 | emitState.connection = _currentConnectionDescription; |
| 631 | emitState.isFailed = _isFailed; |
| 632 | _stateUpdated(emitState); |
| 633 | } |
| 634 |
| 635 | void NativeNetworkingImpl::sctpReadyToSendData() { |
| 636 | } |
| 637 |
| 638 | void NativeNetworkingImpl::sctpDataReceived(const cricket::ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer) { |
| 639 | } |
| 640 |
| 641 | } // namespace tgcalls |
| 642 |