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