| // Compiler errors in conflicting Windows headers if not included here. |
| 6 | #include <winsock2.h> |
| 7 | #endif // WEBRTC_WIN |
| 8 |
| 9 | #include "rtc_base/copy_on_write_buffer.h" |
| 10 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 11 | #include "api/candidate.h" |
| 12 | #include "media/base/media_channel.h" |
| 13 | #include "rtc_base/ssl_fingerprint.h" |
| 14 | #include "pc/sctp_data_channel.h" |
| 15 | #include "p2p/base/port.h" |
| 16 | #include "api/transport/field_trial_based_config.h" |
| 17 |
| 18 | #include <functional> |
| 19 | #include <memory> |
| 20 |
| 21 | #include "Message.h" |
| 22 | #include "ThreadLocalObject.h" |
| 23 | #include "Instance.h" |
| 24 |
| 25 | namespace rtc { |
| 26 | class BasicPacketSocketFactory; |
| 27 | class BasicNetworkManager; |
| 28 | class PacketTransportInternal; |
| 29 | struct NetworkRoute; |
| 30 | } // namespace rtc |
| 31 |
| 32 | namespace cricket { |
| 33 | class BasicPortAllocator; |
| 34 | class P2PTransportChannel; |
| 35 | class IceTransportInternal; |
| 36 | class DtlsTransport; |
| 37 | class RelayPortFactoryInterface; |
| 38 | } // namespace cricket |
| 39 |
| 40 | namespace webrtc { |
| 41 | class TurnCustomizer; |
| 42 | class DtlsSrtpTransport; |
| 43 | class RtpTransport; |
| 44 | class AsyncDnsResolverFactoryInterface; |
| 45 | } // namespace webrtc |
| 46 |
| 47 | namespace tgcalls { |
| 48 |
| 49 | struct Message; |
| 50 | class SctpDataChannelProviderInterfaceImpl; |
| 51 | class Threads; |
| 52 |
| 53 | class NativeNetworkingImpl : public sigslot::has_slots<>, public std::enable_shared_from_this<NativeNetworkingImpl> { |
| 54 | public: |
| 55 | struct RouteDescription { |
| 56 | explicit RouteDescription(std::string const &localDescription_, std::string const &remoteDescription_) : |
| 57 | localDescription(localDescription_), |
| 58 | remoteDescription(remoteDescription_) { |
| 59 | } |
| 60 |
| 61 | std::string localDescription; |
| 62 | std::string remoteDescription; |
| 63 |
| 64 | bool operator==(RouteDescription const &rhs) const { |
| 65 | if (localDescription != rhs.localDescription) { |
| 66 | return false; |
| 67 | } |
| 68 | if (remoteDescription != rhs.remoteDescription) { |
| 69 | return false; |
| 70 | } |
| 71 |
| 72 | return true; |
| 73 | } |
| 74 |
| 75 | bool operator!=(const RouteDescription& rhs) const { |
| 76 | return !(*this == rhs); |
| 77 | } |
| 78 | }; |
| 79 |
| 80 | struct ConnectionDescription { |
| 81 | struct CandidateDescription { |
| 82 | std::string protocol; |
| 83 | std::string type; |
| 84 | std::string address; |
| 85 |
| 86 | bool operator==(CandidateDescription const &rhs) const { |
| 87 | if (protocol != rhs.protocol) { |
| 88 | return false; |
| 89 | } |
| 90 | if (type != rhs.type) { |
| 91 | return false; |
| 92 | } |
| 93 | if (address != rhs.address) { |
| 94 | return false; |
| 95 | } |
| 96 |
| 97 | return true; |
| 98 | } |
| 99 |
| 100 | bool operator!=(const CandidateDescription& rhs) const { |
| 101 | return !(*this == rhs); |
| 102 | } |
| 103 | }; |
| 104 |
| 105 | CandidateDescription local; |
| 106 | CandidateDescription remote; |
| 107 |
| 108 | bool operator==(ConnectionDescription const &rhs) const { |
| 109 | if (local != rhs.local) { |
| 110 | return false; |
| 111 | } |
| 112 | if (remote != rhs.remote) { |
| 113 | return false; |
| 114 | } |
| 115 |
| 116 | return true; |
| 117 | } |
| 118 |
| 119 | bool operator!=(const ConnectionDescription& rhs) const { |
| 120 | return !(*this == rhs); |
| 121 | } |
| 122 | }; |
| 123 |
| 124 | struct State { |
| 125 | bool isReadyToSendData = false; |
| 126 | bool isFailed = false; |
| 127 | absl::optional<RouteDescription> route; |
| 128 | absl::optional<ConnectionDescription> connection; |
| 129 | }; |
| 130 |
| 131 | struct Configuration { |
| 132 | bool isOutgoing = false; |
| 133 | bool enableStunMarking = false; |
| 134 | bool enableTCP = false; |
| 135 | bool enableP2P = false; |
| 136 | std::vector<RtcServer> rtcServers; |
| 137 | absl::optional<Proxy> proxy; |
| 138 | std::function<void(const NativeNetworkingImpl::State &)> stateUpdated; |
| 139 | std::function<void(const cricket::Candidate &)> candidateGathered; |
| 140 | std::function<void(rtc::CopyOnWriteBuffer const &, bool)> transportMessageReceived; |
| 141 | std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> rtcpPacketReceived; |
| 142 | std::function<void(bool)> dataChannelStateUpdated; |
| 143 | std::function<void(std::string const &)> dataChannelMessageReceived; |
| 144 | std::shared_ptr<Threads> threads; |
| 145 | }; |
| 146 |
| 147 | static webrtc::CryptoOptions getDefaulCryptoOptions(); |
| 148 | static ConnectionDescription::CandidateDescription connectionDescriptionFromCandidate(cricket::Candidate const &candidate); |
| 149 |
| 150 | NativeNetworkingImpl(Configuration &&configuration); |
| 151 | ~NativeNetworkingImpl(); |
| 152 |
| 153 | void start(); |
| 154 | void stop(); |
| 155 |
| 156 | PeerIceParameters getLocalIceParameters(); |
| 157 | std::unique_ptr<rtc::SSLFingerprint> getLocalFingerprint(); |
| 158 | void setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup); |
| 159 | void addCandidates(std::vector<cricket::Candidate> const &candidates); |
| 160 |
| 161 | void sendDataChannelMessage(std::string const &message); |
| 162 |
| 163 | webrtc::RtpTransport *getRtpTransport(); |
| 164 |
| 165 | private: |
| 166 | void resetDtlsSrtpTransport(); |
| 167 | void checkConnectionTimeout(); |
| 168 | void candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate); |
| 169 | void candidateGatheringState(cricket::IceTransportInternal *transport); |
| 170 | void OnTransportWritableState_n(rtc::PacketTransportInternal *transport); |
| 171 | void OnTransportReceivingState_n(rtc::PacketTransportInternal *transport); |
| 172 | void transportStateChanged(cricket::IceTransportInternal *transport); |
| 173 | void transportReadyToSend(cricket::IceTransportInternal *transport); |
| 174 | void transportRouteChanged(absl::optional<rtc::NetworkRoute> route); |
| 175 | void candidatePairChanged(cricket::CandidatePairChangeEvent const &event); |
| 176 | void DtlsReadyToSend(bool DtlsReadyToSend); |
| 177 | void UpdateAggregateStates_n(); |
| 178 | void RtpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us, bool isUnresolved); |
| 179 | void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us); |
| 180 |
| 181 | void sctpReadyToSendData(); |
| 182 | void sctpDataReceived(const cricket::ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer); |
| 183 |
| 184 | void notifyStateUpdated(); |
| 185 |
| 186 | std::shared_ptr<Threads> _threads; |
| 187 | bool _isOutgoing = false; |
| 188 | bool _enableStunMarking = false; |
| 189 | bool _enableTCP = false; |
| 190 | bool _enableP2P = false; |
| 191 | std::vector<RtcServer> _rtcServers; |
| 192 | absl::optional<Proxy> _proxy; |
| 193 |
| 194 | std::function<void(const NativeNetworkingImpl::State &)> _stateUpdated; |
| 195 | std::function<void(const cricket::Candidate &)> _candidateGathered; |
| 196 | std::function<void(rtc::CopyOnWriteBuffer const &, bool)> _transportMessageReceived; |
| 197 | std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> _rtcpPacketReceived; |
| 198 | std::function<void(bool)> _dataChannelStateUpdated; |
| 199 | std::function<void(std::string const &)> _dataChannelMessageReceived; |
| 200 |
| 201 | std::unique_ptr<rtc::NetworkMonitorFactory> _networkMonitorFactory; |
| 202 | std::unique_ptr<rtc::BasicPacketSocketFactory> _socketFactory; |
| 203 | std::unique_ptr<rtc::BasicNetworkManager> _networkManager; |
| 204 | std::unique_ptr<webrtc::TurnCustomizer> _turnCustomizer; |
| 205 | std::unique_ptr<cricket::RelayPortFactoryInterface> _relayPortFactory; |
| 206 | std::unique_ptr<cricket::BasicPortAllocator> _portAllocator; |
| 207 | std::unique_ptr<webrtc::AsyncDnsResolverFactoryInterface> _asyncResolverFactory; |
| 208 | std::unique_ptr<cricket::P2PTransportChannel> _transportChannel; |
| 209 | std::unique_ptr<cricket::DtlsTransport> _dtlsTransport; |
| 210 | std::unique_ptr<webrtc::DtlsSrtpTransport> _dtlsSrtpTransport; |
| 211 |
| 212 | std::unique_ptr<SctpDataChannelProviderInterfaceImpl> _dataChannelInterface; |
| 213 |
| 214 | rtc::scoped_refptr<rtc::RTCCertificate> _localCertificate; |
| 215 | PeerIceParameters _localIceParameters; |
| 216 | absl::optional<PeerIceParameters> _remoteIceParameters; |
| 217 |
| 218 | bool _isConnected = false; |
| 219 | bool _isFailed = false; |
| 220 | int64_t _lastDisconnectedTimestamp = 0; |
| 221 | absl::optional<RouteDescription> _currentRouteDescription; |
| 222 | absl::optional<ConnectionDescription> _currentConnectionDescription; |
| 223 | }; |
| 224 |
| 225 | } // namespace tgcalls |
| 226 |
| 227 | #endif |
| 228 |