| // 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 "media/sctp/sctp_transport.h" |
| 14 | #include "pc/sctp_data_channel.h" |
| 15 |
| 16 | #include <functional> |
| 17 | #include <memory> |
| 18 |
| 19 | #include "Message.h" |
| 20 | #include "ThreadLocalObject.h" |
| 21 | #include "Instance.h" |
| 22 |
| 23 | namespace rtc { |
| 24 | class BasicPacketSocketFactory; |
| 25 | class BasicNetworkManager; |
| 26 | class PacketTransportInternal; |
| 27 | struct NetworkRoute; |
| 28 | } // namespace rtc |
| 29 |
| 30 | namespace cricket { |
| 31 | class BasicPortAllocator; |
| 32 | class P2PTransportChannel; |
| 33 | class IceTransportInternal; |
| 34 | class DtlsTransport; |
| 35 | } // namespace cricket |
| 36 |
| 37 | namespace webrtc { |
| 38 | class BasicAsyncResolverFactory; |
| 39 | class TurnCustomizer; |
| 40 | class DtlsSrtpTransport; |
| 41 | class RtpTransport; |
| 42 | } // namespace webrtc |
| 43 |
| 44 | namespace tgcalls { |
| 45 |
| 46 | struct Message; |
| 47 | class SctpDataChannelProviderInterfaceImpl; |
| 48 | class Threads; |
| 49 |
| 50 | class NativeNetworkingImpl : public sigslot::has_slots<>, public std::enable_shared_from_this<NativeNetworkingImpl> { |
| 51 | public: |
| 52 | struct State { |
| 53 | bool isReadyToSendData = false; |
| 54 | bool isFailed = false; |
| 55 | }; |
| 56 |
| 57 | struct Configuration { |
| 58 | bool isOutgoing = false; |
| 59 | bool enableStunMarking = false; |
| 60 | bool enableTCP = false; |
| 61 | bool enableP2P = false; |
| 62 | std::vector<RtcServer> rtcServers; |
| 63 | std::function<void(const NativeNetworkingImpl::State &)> stateUpdated; |
| 64 | std::function<void(const cricket::Candidate &)> candidateGathered; |
| 65 | std::function<void(rtc::CopyOnWriteBuffer const &, bool)> transportMessageReceived; |
| 66 | std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> rtcpPacketReceived; |
| 67 | std::function<void(bool)> dataChannelStateUpdated; |
| 68 | std::function<void(std::string const &)> dataChannelMessageReceived; |
| 69 | std::shared_ptr<Threads> threads; |
| 70 | }; |
| 71 |
| 72 | static webrtc::CryptoOptions getDefaulCryptoOptions(); |
| 73 |
| 74 | NativeNetworkingImpl(Configuration &&configuration); |
| 75 | ~NativeNetworkingImpl(); |
| 76 |
| 77 | void start(); |
| 78 | void stop(); |
| 79 |
| 80 | PeerIceParameters getLocalIceParameters(); |
| 81 | std::unique_ptr<rtc::SSLFingerprint> getLocalFingerprint(); |
| 82 | void setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup); |
| 83 | void addCandidates(std::vector<cricket::Candidate> const &candidates); |
| 84 |
| 85 | void sendDataChannelMessage(std::string const &message); |
| 86 |
| 87 | webrtc::RtpTransport *getRtpTransport(); |
| 88 |
| 89 | private: |
| 90 | void resetDtlsSrtpTransport(); |
| 91 | void checkConnectionTimeout(); |
| 92 | void candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate); |
| 93 | void candidateGatheringState(cricket::IceTransportInternal *transport); |
| 94 | void OnTransportWritableState_n(rtc::PacketTransportInternal *transport); |
| 95 | void OnTransportReceivingState_n(rtc::PacketTransportInternal *transport); |
| 96 | void transportStateChanged(cricket::IceTransportInternal *transport); |
| 97 | void transportReadyToSend(cricket::IceTransportInternal *transport); |
| 98 | void transportPacketReceived(rtc::PacketTransportInternal *transport, const char *bytes, size_t size, const int64_t ×tamp, int unused); |
| 99 | void DtlsReadyToSend(bool DtlsReadyToSend); |
| 100 | void UpdateAggregateStates_n(); |
| 101 | void RtpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us, bool isUnresolved); |
| 102 | void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us); |
| 103 |
| 104 | void sctpReadyToSendData(); |
| 105 | void sctpDataReceived(const cricket::ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer); |
| 106 |
| 107 | std::shared_ptr<Threads> _threads; |
| 108 | bool _isOutgoing = false; |
| 109 | bool _enableStunMarking = false; |
| 110 | bool _enableTCP = false; |
| 111 | bool _enableP2P = false; |
| 112 | std::vector<RtcServer> _rtcServers; |
| 113 |
| 114 | std::function<void(const NativeNetworkingImpl::State &)> _stateUpdated; |
| 115 | std::function<void(const cricket::Candidate &)> _candidateGathered; |
| 116 | std::function<void(rtc::CopyOnWriteBuffer const &, bool)> _transportMessageReceived; |
| 117 | std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> _rtcpPacketReceived; |
| 118 | std::function<void(bool)> _dataChannelStateUpdated; |
| 119 | std::function<void(std::string const &)> _dataChannelMessageReceived; |
| 120 |
| 121 | std::unique_ptr<rtc::BasicPacketSocketFactory> _socketFactory; |
| 122 | std::unique_ptr<rtc::BasicNetworkManager> _networkManager; |
| 123 | std::unique_ptr<webrtc::TurnCustomizer> _turnCustomizer; |
| 124 | std::unique_ptr<cricket::BasicPortAllocator> _portAllocator; |
| 125 | std::unique_ptr<webrtc::BasicAsyncResolverFactory> _asyncResolverFactory; |
| 126 | std::unique_ptr<cricket::P2PTransportChannel> _transportChannel; |
| 127 | std::unique_ptr<cricket::DtlsTransport> _dtlsTransport; |
| 128 | std::unique_ptr<webrtc::DtlsSrtpTransport> _dtlsSrtpTransport; |
| 129 |
| 130 | std::unique_ptr<SctpDataChannelProviderInterfaceImpl> _dataChannelInterface; |
| 131 |
| 132 | rtc::scoped_refptr<rtc::RTCCertificate> _localCertificate; |
| 133 | PeerIceParameters _localIceParameters; |
| 134 | absl::optional<PeerIceParameters> _remoteIceParameters; |
| 135 |
| 136 | bool _isConnected = false; |
| 137 | int64_t _lastNetworkActivityMs = 0; |
| 138 | }; |
| 139 |
| 140 | } // namespace tgcalls |
| 141 |
| 142 | #endif |
| 143 |