Nagram/TMessagesProj/jni/voip/tgcalls/v2/NativeNetworkingImpl.h

225 lines
7.8 KiB
C
Raw Normal View History

2021-06-25 00:43:10 +00:00
#ifndef TGCALLS_NATIVE_NETWORKING_IMPL_H
#define TGCALLS_NATIVE_NETWORKING_IMPL_H
#ifdef WEBRTC_WIN
// Compiler errors in conflicting Windows headers if not included here.
#include <winsock2.h>
#endif // WEBRTC_WIN
#include "rtc_base/copy_on_write_buffer.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "api/candidate.h"
#include "media/base/media_channel.h"
2022-03-11 16:49:54 +00:00
#include "rtc_base/ssl_fingerprint.h"
2021-06-25 00:43:10 +00:00
#include "pc/sctp_data_channel.h"
2022-04-16 14:43:17 +00:00
#include "p2p/base/port.h"
2021-06-25 00:43:10 +00:00
#include <functional>
#include <memory>
#include "Message.h"
#include "ThreadLocalObject.h"
#include "Instance.h"
namespace rtc {
class BasicPacketSocketFactory;
class BasicNetworkManager;
class PacketTransportInternal;
struct NetworkRoute;
} // namespace rtc
namespace cricket {
class BasicPortAllocator;
class P2PTransportChannel;
class IceTransportInternal;
class DtlsTransport;
} // namespace cricket
namespace webrtc {
class TurnCustomizer;
class DtlsSrtpTransport;
class RtpTransport;
2022-04-16 14:43:17 +00:00
class AsyncDnsResolverFactoryInterface;
2021-06-25 00:43:10 +00:00
} // namespace webrtc
namespace tgcalls {
struct Message;
class SctpDataChannelProviderInterfaceImpl;
class Threads;
class NativeNetworkingImpl : public sigslot::has_slots<>, public std::enable_shared_from_this<NativeNetworkingImpl> {
public:
2022-04-16 14:43:17 +00:00
struct RouteDescription {
explicit RouteDescription(std::string const &localDescription_, std::string const &remoteDescription_) :
localDescription(localDescription_),
remoteDescription(remoteDescription_) {
}
std::string localDescription;
std::string remoteDescription;
bool operator==(RouteDescription const &rhs) const {
if (localDescription != rhs.localDescription) {
return false;
}
if (remoteDescription != rhs.remoteDescription) {
return false;
}
return true;
}
bool operator!=(const RouteDescription& rhs) const {
return !(*this == rhs);
}
};
struct ConnectionDescription {
struct CandidateDescription {
std::string protocol;
std::string type;
std::string address;
bool operator==(CandidateDescription const &rhs) const {
if (protocol != rhs.protocol) {
return false;
}
if (type != rhs.type) {
return false;
}
if (address != rhs.address) {
return false;
}
return true;
}
bool operator!=(const CandidateDescription& rhs) const {
return !(*this == rhs);
}
};
CandidateDescription local;
CandidateDescription remote;
bool operator==(ConnectionDescription const &rhs) const {
if (local != rhs.local) {
return false;
}
if (remote != rhs.remote) {
return false;
}
return true;
}
bool operator!=(const ConnectionDescription& rhs) const {
return !(*this == rhs);
}
};
2021-06-25 00:43:10 +00:00
struct State {
bool isReadyToSendData = false;
bool isFailed = false;
2022-04-16 14:43:17 +00:00
absl::optional<RouteDescription> route;
absl::optional<ConnectionDescription> connection;
2021-06-25 00:43:10 +00:00
};
struct Configuration {
bool isOutgoing = false;
bool enableStunMarking = false;
bool enableTCP = false;
bool enableP2P = false;
std::vector<RtcServer> rtcServers;
2022-04-16 14:43:17 +00:00
absl::optional<Proxy> proxy;
2021-06-25 00:43:10 +00:00
std::function<void(const NativeNetworkingImpl::State &)> stateUpdated;
std::function<void(const cricket::Candidate &)> candidateGathered;
std::function<void(rtc::CopyOnWriteBuffer const &, bool)> transportMessageReceived;
std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> rtcpPacketReceived;
std::function<void(bool)> dataChannelStateUpdated;
std::function<void(std::string const &)> dataChannelMessageReceived;
std::shared_ptr<Threads> threads;
};
static webrtc::CryptoOptions getDefaulCryptoOptions();
NativeNetworkingImpl(Configuration &&configuration);
~NativeNetworkingImpl();
void start();
void stop();
PeerIceParameters getLocalIceParameters();
std::unique_ptr<rtc::SSLFingerprint> getLocalFingerprint();
void setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup);
void addCandidates(std::vector<cricket::Candidate> const &candidates);
void sendDataChannelMessage(std::string const &message);
webrtc::RtpTransport *getRtpTransport();
private:
void resetDtlsSrtpTransport();
void checkConnectionTimeout();
void candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate);
void candidateGatheringState(cricket::IceTransportInternal *transport);
void OnTransportWritableState_n(rtc::PacketTransportInternal *transport);
void OnTransportReceivingState_n(rtc::PacketTransportInternal *transport);
void transportStateChanged(cricket::IceTransportInternal *transport);
void transportReadyToSend(cricket::IceTransportInternal *transport);
void transportPacketReceived(rtc::PacketTransportInternal *transport, const char *bytes, size_t size, const int64_t &timestamp, int unused);
2022-04-16 14:43:17 +00:00
void transportRouteChanged(absl::optional<rtc::NetworkRoute> route);
void candidatePairChanged(cricket::CandidatePairChangeEvent const &event);
2021-06-25 00:43:10 +00:00
void DtlsReadyToSend(bool DtlsReadyToSend);
void UpdateAggregateStates_n();
void RtpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us, bool isUnresolved);
void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us);
void sctpReadyToSendData();
void sctpDataReceived(const cricket::ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer);
2022-04-16 14:43:17 +00:00
void notifyStateUpdated();
2021-06-25 00:43:10 +00:00
std::shared_ptr<Threads> _threads;
bool _isOutgoing = false;
bool _enableStunMarking = false;
bool _enableTCP = false;
bool _enableP2P = false;
std::vector<RtcServer> _rtcServers;
2022-04-16 14:43:17 +00:00
absl::optional<Proxy> _proxy;
2021-06-25 00:43:10 +00:00
std::function<void(const NativeNetworkingImpl::State &)> _stateUpdated;
std::function<void(const cricket::Candidate &)> _candidateGathered;
std::function<void(rtc::CopyOnWriteBuffer const &, bool)> _transportMessageReceived;
std::function<void(rtc::CopyOnWriteBuffer const &, int64_t)> _rtcpPacketReceived;
std::function<void(bool)> _dataChannelStateUpdated;
std::function<void(std::string const &)> _dataChannelMessageReceived;
2022-04-16 14:43:17 +00:00
std::unique_ptr<rtc::NetworkMonitorFactory> _networkMonitorFactory;
2021-06-25 00:43:10 +00:00
std::unique_ptr<rtc::BasicPacketSocketFactory> _socketFactory;
std::unique_ptr<rtc::BasicNetworkManager> _networkManager;
std::unique_ptr<webrtc::TurnCustomizer> _turnCustomizer;
std::unique_ptr<cricket::BasicPortAllocator> _portAllocator;
2022-04-16 14:43:17 +00:00
std::unique_ptr<webrtc::AsyncDnsResolverFactoryInterface> _asyncResolverFactory;
2021-06-25 00:43:10 +00:00
std::unique_ptr<cricket::P2PTransportChannel> _transportChannel;
std::unique_ptr<cricket::DtlsTransport> _dtlsTransport;
std::unique_ptr<webrtc::DtlsSrtpTransport> _dtlsSrtpTransport;
std::unique_ptr<SctpDataChannelProviderInterfaceImpl> _dataChannelInterface;
rtc::scoped_refptr<rtc::RTCCertificate> _localCertificate;
PeerIceParameters _localIceParameters;
absl::optional<PeerIceParameters> _remoteIceParameters;
bool _isConnected = false;
2022-04-16 14:43:17 +00:00
bool _isFailed = false;
2021-06-25 00:43:10 +00:00
int64_t _lastNetworkActivityMs = 0;
2022-04-16 14:43:17 +00:00
absl::optional<RouteDescription> _currentRouteDescription;
absl::optional<ConnectionDescription> _currentConnectionDescription;
2021-06-25 00:43:10 +00:00
};
} // namespace tgcalls
#endif