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