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