| #include <vector> |
| 6 | #include <string> |
| 7 | #include <memory> |
| 8 | #include <map> |
| 9 |
| 10 | namespace rtc { |
| 11 | template <typename VideoFrameT> |
| 12 | class VideoSinkInterface; |
| 13 | } // namespace rtc |
| 14 |
| 15 | namespace webrtc { |
| 16 | class VideoFrame; |
| 17 | } // namespace webrtc |
| 18 |
| 19 | namespace tgcalls { |
| 20 |
| 21 | class VideoCaptureInterface; |
| 22 | class PlatformContext; |
| 23 |
| 24 | struct Proxy { |
| 25 | std::string host; |
| 26 | uint16_t port = 0; |
| 27 | std::string login; |
| 28 | std::string password; |
| 29 | }; |
| 30 |
| 31 | struct RtcServer { |
| 32 | std::string host; |
| 33 | uint16_t port = 0; |
| 34 | std::string login; |
| 35 | std::string password; |
| 36 | bool isTurn = false; |
| 37 | }; |
| 38 |
| 39 | enum class EndpointType { |
| 40 | Inet, |
| 41 | Lan, |
| 42 | UdpRelay, |
| 43 | TcpRelay |
| 44 | }; |
| 45 |
| 46 | struct EndpointHost { |
| 47 | std::string ipv4; |
| 48 | std::string ipv6; |
| 49 | }; |
| 50 |
| 51 | struct Endpoint { |
| 52 | int64_t endpointId = 0; |
| 53 | EndpointHost host; |
| 54 | uint16_t port = 0; |
| 55 | EndpointType type = EndpointType{}; |
| 56 | unsigned char peerTag[16] = { 0 }; |
| 57 | }; |
| 58 |
| 59 | enum class ProtocolVersion { |
| 60 | V0, |
| 61 | V1 // Low-cost network negotiation |
| 62 | }; |
| 63 |
| 64 | enum class NetworkType { |
| 65 | Unknown, |
| 66 | Gprs, |
| 67 | Edge, |
| 68 | ThirdGeneration, |
| 69 | Hspa, |
| 70 | Lte, |
| 71 | WiFi, |
| 72 | Ethernet, |
| 73 | OtherHighSpeed, |
| 74 | OtherLowSpeed, |
| 75 | OtherMobile, |
| 76 | Dialup |
| 77 | }; |
| 78 |
| 79 | enum class DataSaving { |
| 80 | Never, |
| 81 | Mobile, |
| 82 | Always |
| 83 | }; |
| 84 |
| 85 | struct PersistentState { |
| 86 | std::vector<uint8_t> value; |
| 87 | }; |
| 88 |
| 89 | struct Config { |
| 90 | double initializationTimeout = 0.; |
| 91 | double receiveTimeout = 0.; |
| 92 | DataSaving dataSaving = DataSaving::Never; |
| 93 | bool enableP2P = false; |
| 94 | bool enableAEC = false; |
| 95 | bool enableNS = false; |
| 96 | bool enableAGC = false; |
| 97 | bool enableCallUpgrade = false; |
| 98 | bool enableVolumeControl = false; |
| 99 | #ifndef _WIN32 |
| 100 | std::string logPath; |
| 101 | #else |
| 102 | std::wstring logPath; |
| 103 | #endif |
| 104 | int maxApiLayer = 0; |
| 105 | float preferredAspectRatio; |
| 106 | bool enableHighBitrateVideo = false; |
| 107 | std::vector<std::string> preferredVideoCodecs; |
| 108 | ProtocolVersion protocolVersion = ProtocolVersion::V0; |
| 109 | }; |
| 110 |
| 111 | struct EncryptionKey { |
| 112 | static constexpr int kSize = 256; |
| 113 |
| 114 | std::shared_ptr<const std::array<uint8_t, kSize>> value; |
| 115 | bool isOutgoing = false; |
| 116 |
| 117 | EncryptionKey( |
| 118 | std::shared_ptr<std::array<uint8_t, kSize>> value, |
| 119 | bool isOutgoing) |
| 120 | : value(std::move(value)), isOutgoing(isOutgoing) { |
| 121 | } |
| 122 | }; |
| 123 |
| 124 | enum class State { |
| 125 | WaitInit, |
| 126 | WaitInitAck, |
| 127 | Established, |
| 128 | Failed, |
| 129 | Reconnecting |
| 130 | }; |
| 131 |
| 132 | // Defined in VideoCaptureInterface.h |
| 133 | enum class VideoState; |
| 134 |
| 135 | enum class AudioState { |
| 136 | Muted, |
| 137 | Active, |
| 138 | }; |
| 139 |
| 140 | struct TrafficStats { |
| 141 | uint64_t bytesSentWifi = 0; |
| 142 | uint64_t bytesReceivedWifi = 0; |
| 143 | uint64_t bytesSentMobile = 0; |
| 144 | uint64_t bytesReceivedMobile = 0; |
| 145 | }; |
| 146 |
| 147 | struct FinalState { |
| 148 | PersistentState persistentState; |
| 149 | std::string debugLog; |
| 150 | TrafficStats trafficStats; |
| 151 | bool isRatingSuggested = false; |
| 152 | }; |
| 153 |
| 154 | class Instance { |
| 155 | protected: |
| 156 | Instance() = default; |
| 157 |
| 158 | public: |
| 159 | virtual ~Instance() = default; |
| 160 |
| 161 | virtual void setNetworkType(NetworkType networkType) = 0; |
| 162 | virtual void setMuteMicrophone(bool muteMicrophone) = 0; |
| 163 | virtual void setAudioOutputGainControlEnabled(bool enabled) = 0; |
| 164 | virtual void setEchoCancellationStrength(int strength) = 0; |
| 165 |
| 166 | virtual void setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) = 0; |
| 167 |
| 168 | virtual void setAudioInputDevice(std::string id) = 0; |
| 169 | virtual void setAudioOutputDevice(std::string id) = 0; |
| 170 | virtual void setInputVolume(float level) = 0; |
| 171 | virtual void setOutputVolume(float level) = 0; |
| 172 | virtual void setAudioOutputDuckingEnabled(bool enabled) = 0; |
| 173 |
| 174 | virtual void setIsLowBatteryLevel(bool isLowBatteryLevel) = 0; |
| 175 |
| 176 | virtual std::string getLastError() = 0; |
| 177 | virtual std::string getDebugInfo() = 0; |
| 178 | virtual int64_t getPreferredRelayId() = 0; |
| 179 | virtual TrafficStats getTrafficStats() = 0; |
| 180 | virtual PersistentState getPersistentState() = 0; |
| 181 |
| 182 | virtual void receiveSignalingData(const std::vector<uint8_t> &data) = 0; |
| 183 | virtual void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) = 0; |
| 184 |
| 185 | virtual void stop(std::function<void(FinalState)> completion) = 0; |
| 186 |
| 187 | }; |
| 188 |
| 189 | template <typename Implementation> |
| 190 | bool Register(); |
| 191 |
| 192 | struct Descriptor { |
| 193 | Config config; |
| 194 | PersistentState persistentState; |
| 195 | std::vector<Endpoint> endpoints; |
| 196 | std::unique_ptr<Proxy> proxy; |
| 197 | std::vector<RtcServer> rtcServers; |
| 198 | NetworkType initialNetworkType = NetworkType(); |
| 199 | EncryptionKey encryptionKey; |
| 200 | std::shared_ptr<VideoCaptureInterface> videoCapture; |
| 201 | std::function<void(State)> stateUpdated; |
| 202 | std::function<void(int)> signalBarsUpdated; |
| 203 | std::function<void(bool)> remoteBatteryLevelIsLowUpdated; |
| 204 | std::function<void(AudioState, VideoState)> remoteMediaStateUpdated; |
| 205 | std::function<void(float)> remotePrefferedAspectRatioUpdated; |
| 206 | std::function<void(const std::vector<uint8_t> &)> signalingDataEmitted; |
| 207 | std::shared_ptr<PlatformContext> platformContext; |
| 208 | }; |
| 209 |
| 210 | class Meta { |
| 211 | public: |
| 212 | virtual ~Meta() = default; |
| 213 |
| 214 | virtual std::unique_ptr<Instance> construct(Descriptor &&descriptor) = 0; |
| 215 | virtual int connectionMaxLayer() = 0; |
| 216 | virtual std::vector<std::string> versions() = 0; |
| 217 |
| 218 | static std::unique_ptr<Instance> Create( |
| 219 | const std::string &version, |
| 220 | Descriptor &&descriptor); |
| 221 | static std::vector<std::string> Versions(); |
| 222 | static int MaxLayer(); |
| 223 |
| 224 | private: |
| 225 | template <typename Implementation> |
| 226 | friend bool Register(); |
| 227 |
| 228 | template <typename Implementation> |
| 229 | static bool RegisterOne(); |
| 230 | static void RegisterOne(std::shared_ptr<Meta> meta); |
| 231 |
| 232 | }; |
| 233 |
| 234 | template <typename Implementation> |
| 235 | bool Meta::RegisterOne() { |
| 236 | class MetaImpl final : public Meta { |
| 237 | public: |
| 238 | int connectionMaxLayer() override { |
| 239 | return Implementation::GetConnectionMaxLayer(); |
| 240 | } |
| 241 | std::vector<std::string> versions() override { |
| 242 | return Implementation::GetVersions(); |
| 243 | } |
| 244 | std::unique_ptr<Instance> construct(Descriptor &&descriptor) override { |
| 245 | return std::make_unique<Implementation>(std::move(descriptor)); |
| 246 | } |
| 247 | }; |
| 248 | RegisterOne(std::make_shared<MetaImpl>()); |
| 249 | return true; |
| 250 | } |
| 251 |
| 252 | void SetLoggingFunction(std::function<void(std::string const &)> loggingFunction); |
| 253 |
| 254 | } // namespace tgcalls |
| 255 |
| 256 | #endif |
| 257 |