| 6 |
| 7 | #ifndef VOIPCONTROLLER_H |
| 8 | #define VOIPCONTROLLER_H |
| 9 |
| 10 | #include "utils.h" |
| 11 | #include "AudioDevice.h" |
| 12 | #include "BlockingQueue.h" |
| 13 | #include "Buffers.h" |
| 14 | #include "CongestionControl.h" |
| 15 | #include "EchoCanceller.h" |
| 16 | #include "Endpoint.h" |
| 17 | #include "JitterBuffer.h" |
| 18 | #include "MessageThread.h" |
| 19 | #include "NetworkSocket.h" |
| 20 | #include "OpusDecoder.h" |
| 21 | #include "OpusEncoder.h" |
| 22 | #include "PacketReassembler.h" |
| 23 | #include "VoIPControllerDeclarations.h" |
| 24 | #include "audio/AudioIO.h" |
| 25 | #include "audio/AudioInput.h" |
| 26 | #include "audio/AudioOutput.h" |
| 27 | #include "video/ScreamCongestionController.h" |
| 28 | #include "video/VideoRenderer.h" |
| 29 | #include "video/VideoSource.h" |
| 30 |
| 31 | #ifndef _WIN32 |
| 32 | #include <arpa/inet.h> |
| 33 | #include <netinet/in.h> |
| 34 | #endif |
| 35 | #ifdef __APPLE__ |
| 36 | #include "os/darwin/AudioUnitIO.h" |
| 37 | #include <TargetConditionals.h> |
| 38 | #endif |
| 39 |
| 40 | #include <atomic> |
| 41 | #include <map> |
| 42 | #include <memory> |
| 43 | #include <cstdint> |
| 44 | #include <string> |
| 45 | #include <unordered_map> |
| 46 | #include <vector> |
| 47 | #include <deque> |
| 48 |
| 49 | #define LIBTGVOIP_VERSION "2.7" |
| 50 |
| 51 | #ifdef _WIN32 |
| 52 | #undef GetCurrentTime |
| 53 | #undef Error::TIMEOUT |
| 54 | #endif |
| 55 |
| 56 | namespace tgvoip |
| 57 | { |
| 58 |
| 59 | class VoIPController |
| 60 | { |
| 61 | friend class VoIPGroupController; |
| 62 | friend class PacketSender; |
| 63 |
| 64 | public: |
| 65 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(VoIPController); |
| 66 | struct Config |
| 67 | { |
| 68 | Config(double initTimeout = 30.0, double recvTimeout = 20.0, DataSaving dataSaving = DataSaving::NEVER, |
| 69 | bool enableAEC = false, bool enableNS = false, bool enableAGC = false, bool enableCallUpgrade = false); |
| 70 |
| 71 | double initTimeout; |
| 72 | double recvTimeout; |
| 73 | DataSaving dataSaving; |
| 74 | #ifndef _WIN32 |
| 75 | std::string logFilePath = ""; |
| 76 | std::string statsDumpFilePath = ""; |
| 77 | #else |
| 78 | std::wstring logFilePath = L""; |
| 79 | std::wstring statsDumpFilePath = L""; |
| 80 | #endif |
| 81 |
| 82 | bool enableAEC; |
| 83 | bool enableNS; |
| 84 | bool enableAGC; |
| 85 |
| 86 | bool enableCallUpgrade; |
| 87 |
| 88 | bool logPacketStats = false; |
| 89 | bool enableVolumeControl = false; |
| 90 |
| 91 | bool enableVideoSend = false; |
| 92 | bool enableVideoReceive = false; |
| 93 | }; |
| 94 |
| 95 | struct TrafficStats |
| 96 | { |
| 97 | std::uint64_t bytesSentWifi; |
| 98 | std::uint64_t bytesRecvdWifi; |
| 99 | std::uint64_t bytesSentMobile; |
| 100 | std::uint64_t bytesRecvdMobile; |
| 101 | }; |
| 102 |
| 103 | struct PendingOutgoingPacket |
| 104 | { |
| 105 | PendingOutgoingPacket(std::uint32_t seq, PktType type, std::size_t len, Buffer&& data, std::int64_t endpoint); |
| 106 | PendingOutgoingPacket(PendingOutgoingPacket&& other) noexcept; |
| 107 | PendingOutgoingPacket& operator=(PendingOutgoingPacket&& other) noexcept; |
| 108 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(PendingOutgoingPacket); |
| 109 |
| 110 | std::uint32_t seq; |
| 111 | PktType type; |
| 112 | std::size_t len; |
| 113 | Buffer data; |
| 114 | std::int64_t endpoint; |
| 115 | }; |
| 116 |
| 117 | struct Stream |
| 118 | { |
| 119 | std::int32_t userID; |
| 120 | std::uint8_t id; |
| 121 | StreamType type; |
| 122 | std::uint32_t codec; |
| 123 | bool enabled; |
| 124 | bool extraECEnabled; |
| 125 | std::uint16_t frameDuration; |
| 126 | std::shared_ptr<JitterBuffer> jitterBuffer; |
| 127 | std::shared_ptr<OpusDecoder> decoder; |
| 128 | std::shared_ptr<PacketReassembler> packetReassembler; |
| 129 | std::shared_ptr<CallbackWrapper> callbackWrapper; |
| 130 | std::vector<Buffer> codecSpecificData; |
| 131 | bool csdIsValid = false; |
| 132 | bool paused = false; |
| 133 | int resolution; |
| 134 | unsigned int width = 0; |
| 135 | unsigned int height = 0; |
| 136 | std::uint16_t rotation = 0; |
| 137 | }; |
| 138 |
| 139 | struct ProtocolInfo |
| 140 | { |
| 141 | std::uint32_t version; |
| 142 | InitVideoRes maxVideoResolution; |
| 143 | std::vector<std::uint32_t> videoDecoders; |
| 144 | bool videoCaptureSupported; |
| 145 | bool videoDisplaySupported; |
| 146 | bool callUpgradeSupported; |
| 147 | }; |
| 148 |
| 149 | /** |
| 150 | * Use this field to store any of your context data associated with this call |
| 151 | */ |
| 152 | void* implData; |
| 153 |
| 154 | VoIPController(); |
| 155 | virtual ~VoIPController(); |
| 156 |
| 157 | /** |
| 158 | * Set the initial endpoints (relays) |
| 159 | * @param endpoints Endpoints converted from phone.PhoneConnection TL objects |
| 160 | * @param allowP2p Whether p2p connectivity is allowed |
| 161 | * @param connectionMaxLayer The max_layer field from the phoneCallProtocol object returned by Telegram server. |
| 162 | * DO NOT HARDCODE THIS VALUE, it's extremely important for backwards compatibility. |
| 163 | */ |
| 164 | void SetRemoteEndpoints(const std::vector<Endpoint>& m_endpoints, bool m_allowP2p, std::int32_t m_connectionMaxLayer); |
| 165 |
| 166 | /** |
| 167 | * Initialize and start all the internal threads |
| 168 | */ |
| 169 | void Start(); |
| 170 |
| 171 | /** |
| 172 | * Stop any internal threads. Don't call any other methods after this. |
| 173 | */ |
| 174 | void Stop(); |
| 175 |
| 176 | /** |
| 177 | * Initiate connection |
| 178 | */ |
| 179 | void Connect(); |
| 180 |
| 181 | Endpoint& GetRemoteEndpoint(); |
| 182 |
| 183 | /** |
| 184 | * Get the debug info string to be displayed in client UI |
| 185 | */ |
| 186 | virtual std::string GetDebugString(); |
| 187 |
| 188 | /** |
| 189 | * Notify the library of network type change |
| 190 | * @param type The new network type |
| 191 | */ |
| 192 | virtual void SetNetworkType(NetType type); |
| 193 |
| 194 | /** |
| 195 | * Get the average round-trip time for network packets |
| 196 | * @return |
| 197 | */ |
| 198 | double GetAverageRTT(); |
| 199 |
| 200 | static double GetCurrentTime(); |
| 201 |
| 202 | virtual void SetMicMute(bool mute); |
| 203 |
| 204 | void SetEncryptionKey(char* key, bool m_isOutgoing); |
| 205 |
| 206 | void SetConfig(const Config& cfg); |
| 207 | void DebugCtl(int request, int param); |
| 208 | void GetStats(TrafficStats* m_stats); |
| 209 |
| 210 | std::int64_t GetPreferredRelayID(); |
| 211 | Error GetLastError(); |
| 212 |
| 213 | static CryptoFunctions crypto; |
| 214 | static const char* GetVersion(); |
| 215 | std::string GetDebugLog(); |
| 216 |
| 217 | static std::vector<AudioInputDevice> EnumerateAudioInputs(); |
| 218 | static std::vector<AudioOutputDevice> EnumerateAudioOutputs(); |
| 219 |
| 220 | void SetCurrentAudioInput(std::string id); |
| 221 | void SetCurrentAudioOutput(std::string id); |
| 222 |
| 223 | std::string GetCurrentAudioInputID() const; |
| 224 | std::string GetCurrentAudioOutputID() const; |
| 225 |
| 226 | /** |
| 227 | * Set the proxy server to route the data through. Call this before connecting. |
| 228 | * @param protocol Proxy::NONE or Proxy::SOCKS5 |
| 229 | * @param address IP address or domain name of the server |
| 230 | * @param port Port of the server |
| 231 | * @param username Username; empty string for anonymous |
| 232 | * @param password Password; empty string if none |
| 233 | */ |
| 234 | void SetProxy(Proxy protocol, std::string address, std::uint16_t port, std::string username, std::string password); |
| 235 |
| 236 | /** |
| 237 | * Get the number of signal bars to display in the client UI. |
| 238 | * @return the number of signal bars, from 1 to 4 |
| 239 | */ |
| 240 | int GetSignalBarsCount(); |
| 241 |
| 242 | /** |
| 243 | * Enable or disable AGC (automatic gain control) on audio output. Should only be enabled on phones when the earpiece speaker is being used. |
| 244 | * The audio output will be louder with this on. |
| 245 | * AGC with speakerphone or other kinds of loud speakers has detrimental effects on some echo cancellation implementations. |
| 246 | * @param enabled I usually pick argument names to be self-explanatory |
| 247 | */ |
| 248 | void SetAudioOutputGainControlEnabled(bool enabled); |
| 249 |
| 250 | /** |
| 251 | * Get the additional capabilities of the peer client app |
| 252 | * @return corresponding TGVOIP_PEER_CAP_* flags OR'ed together |
| 253 | */ |
| 254 | std::uint32_t GetPeerCapabilities(); |
| 255 |
| 256 | /** |
| 257 | * Send the peer the key for the group call to prepare this private call to an upgrade to a E2E group call. |
| 258 | * The peer must have the TGVOIP_PEER_CAP_GROUP_CALLS capability. After the peer acknowledges the key, Callbacks::groupCallKeySent will be called. |
| 259 | * @param key newly-generated group call key, must be exactly 265 bytes long |
| 260 | */ |
| 261 | void SendGroupCallKey(std::uint8_t* key); |
| 262 |
| 263 | /** |
| 264 | * In an incoming call, request the peer to generate a new encryption key, send it to you and upgrade this call to a E2E group call. |
| 265 | */ |
| 266 | void RequestCallUpgrade(); |
| 267 |
| 268 | void SetEchoCancellationStrength(int strength); |
| 269 | State GetConnectionState() const; |
| 270 | bool NeedRate(); |
| 271 |
| 272 | /** |
| 273 | * Get the maximum connection layer supported by this libtgvoip version. |
| 274 | * Pass this as <code>max_layer</code> in the phone.phoneConnection TL object when requesting and accepting calls. |
| 275 | */ |
| 276 | static std::int32_t GetConnectionMaxLayer(); |
| 277 |
| 278 | /** |
| 279 | * Get the persistable state of the library, like proxy capabilities, to save somewhere on the disk. Call this at the end of the call. |
| 280 | * Using this will speed up the connection establishment in some cases. |
| 281 | */ |
| 282 | std::vector<std::uint8_t> GetPersistentState(); |
| 283 |
| 284 | /** |
| 285 | * Load the persistable state. Call this before starting the call. |
| 286 | */ |
| 287 | void SetPersistentState(const std::vector<std::uint8_t>& m_state); |
| 288 |
| 289 | #if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) |
| 290 | void SetAudioDataCallbacks(std::function<void(std::int16_t*, std::size_t)> input, std::function<void(std::int16_t*, std::size_t)> output, std::function<void(std::int16_t*, std::size_t)> preprocessed); |
| 291 | #endif |
| 292 |
| 293 | void SetVideoCodecSpecificData(const std::vector<Buffer>& data); |
| 294 |
| 295 | struct Callbacks |
| 296 | { |
| 297 | void (*connectionStateChanged)(VoIPController*, State); |
| 298 | void (*signalBarCountChanged)(VoIPController*, int); |
| 299 | void (*groupCallKeySent)(VoIPController*); |
| 300 | void (*groupCallKeyReceived)(VoIPController*, const std::uint8_t*); |
| 301 | void (*upgradeToGroupCallRequested)(VoIPController*); |
| 302 | }; |
| 303 | void SetCallbacks(Callbacks m_callbacks); |
| 304 |
| 305 | float GetOutputLevel() const; |
| 306 |
| 307 | void SetVideoSource(video::VideoSource* source); |
| 308 | void SetVideoRenderer(video::VideoRenderer* renderer); |
| 309 |
| 310 | void SetInputVolume(float level); |
| 311 | void SetOutputVolume(float level); |
| 312 | #if defined(__APPLE__) && defined(TARGET_OS_OSX) |
| 313 | void SetAudioOutputDuckingEnabled(bool enabled); |
| 314 | #endif |
| 315 |
| 316 | #ifdef __APPLE__ |
| 317 | static double machTimebase; |
| 318 | static std::uint64_t machTimestart; |
| 319 | #endif |
| 320 | #ifdef _WIN32 |
| 321 | static std::int64_t win32TimeScale; |
| 322 | static bool didInitWin32TimeScale; |
| 323 | #endif |
| 324 |
| 325 | protected: |
| 326 | struct RecentOutgoingPacket |
| 327 | { |
| 328 | std::uint32_t seq; |
| 329 | std::uint16_t id; // for group calls only |
| 330 | double sendTime; |
| 331 | double ackTime; |
| 332 | PktType type; |
| 333 | std::uint32_t size; |
| 334 | PacketSender* sender; |
| 335 | bool lost; |
| 336 | }; |
| 337 |
| 338 | struct QueuedPacket |
| 339 | { |
| 340 | Buffer data; |
| 341 | PktType type; |
| 342 | HistoricBuffer<std::uint32_t, 16> seqs; |
| 343 | double firstSentTime; |
| 344 | double lastSentTime; |
| 345 | double retryInterval; |
| 346 | double timeout; |
| 347 | }; |
| 348 |
| 349 | virtual void ProcessIncomingPacket(NetworkPacket& packet, Endpoint& srcEndpoint); |
| 350 | virtual void ProcessExtraData(Buffer& data); |
| 351 | virtual void WritePacketHeader(std::uint32_t pseq, BufferOutputStream* s, PktType type, std::uint32_t length, PacketSender* source); |
| 352 | virtual void SendPacket(std::uint8_t* data, std::size_t len, Endpoint& ep, PendingOutgoingPacket& srcPacket); |
| 353 | virtual void SendInit(); |
| 354 | virtual void SendUdpPing(Endpoint& endpoint); |
| 355 | virtual void SendRelayPings(); |
| 356 | virtual void OnAudioOutputReady(); |
| 357 | virtual void SendExtra(Buffer& data, ExtraType type); |
| 358 | void SendStreamFlags(Stream& stream); |
| 359 | void InitializeTimers(); |
| 360 | void ResetEndpointPingStats(); |
| 361 | void SendVideoFrame(const Buffer& frame, std::uint32_t flags, std::uint32_t rotation); |
| 362 | void ProcessIncomingVideoFrame(Buffer frame, std::uint32_t pts, bool keyframe, std::uint16_t rotation); |
| 363 | std::shared_ptr<Stream> GetStreamByType(StreamType, bool outgoing) const; |
| 364 | std::shared_ptr<Stream> GetStreamByID(std::uint8_t id, bool outgoing) const; |
| 365 | Endpoint* GetEndpointForPacket(const PendingOutgoingPacket& pkt); |
| 366 | bool SendOrEnqueuePacket(PendingOutgoingPacket pkt, bool enqueue = true, PacketSender* source = nullptr); |
| 367 | static std::string NetworkTypeToString(NetType type); |
| 368 | CellularCarrierInfo GetCarrierInfo(); |
| 369 |
| 370 | private: |
| 371 | struct UnacknowledgedExtraData; |
| 372 |
| 373 | struct UnacknowledgedExtraData |
| 374 | { |
| 375 | ExtraType type; |
| 376 | Buffer data; |
| 377 | std::uint32_t firstContainingSeq; |
| 378 | }; |
| 379 |
| 380 | struct RecentIncomingPacket |
| 381 | { |
| 382 | std::uint32_t seq; |
| 383 | double recvTime; |
| 384 | }; |
| 385 |
| 386 | struct DebugLoggedPacket |
| 387 | { |
| 388 | std::int32_t seq; |
| 389 | double timestamp; |
| 390 | std::int32_t length; |
| 391 | }; |
| 392 |
| 393 | struct RawPendingOutgoingPacket |
| 394 | { |
| 395 | TGVOIP_MOVE_ONLY(RawPendingOutgoingPacket); |
| 396 | NetworkPacket packet; |
| 397 | std::shared_ptr<NetworkSocket> socket; |
| 398 | }; |
| 399 |
| 400 | enum class UdpState |
| 401 | { |
| 402 | UNKNOWN = 0, |
| 403 | PING_PENDING, |
| 404 | PING_SENT, |
| 405 | AVAILABLE, |
| 406 | NOT_AVAILABLE, |
| 407 | BAD, |
| 408 | }; |
| 409 |
| 410 | void RunRecvThread(); |
| 411 | void RunSendThread(); |
| 412 | void HandleAudioInput(std::uint8_t* data, std::size_t len, std::uint8_t* secondaryData, std::size_t secondaryLen); |
| 413 | void UpdateAudioBitrateLimit(); |
| 414 | void SetState(State m_state); |
| 415 | void UpdateAudioOutputState(); |
| 416 | void InitUDPProxy(); |
| 417 | void UpdateDataSavingState(); |
| 418 | void KDF(std::uint8_t* msgKey, std::size_t x, std::uint8_t* aesKey, std::uint8_t* aesIv); |
| 419 | void KDF2(std::uint8_t* msgKey, std::size_t x, std::uint8_t* aesKey, std::uint8_t* aesIv); |
| 420 | void SendPublicEndpointsRequest(); |
| 421 | void SendPublicEndpointsRequest(const Endpoint& relay); |
| 422 | Endpoint& GetEndpointByType(Endpoint::Type type); |
| 423 | void SendPacketReliably(PktType type, std::uint8_t* data, std::size_t len, double retryInterval, double timeout); |
| 424 | std::uint32_t GenerateOutSeq(); |
| 425 | void ActuallySendPacket(NetworkPacket pkt, Endpoint& ep); |
| 426 | void InitializeAudio(); |
| 427 | void StartAudio(); |
| 428 | void ProcessRelaySpecialRequest(BufferInputStream& in, Endpoint& srcEndpoint); |
| 429 | void ProcessAcknowledgedOutgoingExtra(UnacknowledgedExtraData& extra); |
| 430 | void AddIPv6Relays(); |
| 431 | void AddTCPRelays(); |
| 432 | void SendUdpPings(); |
| 433 | void EvaluateUdpPingResults(); |
| 434 | void UpdateRTT(); |
| 435 | void UpdateCongestion(); |
| 436 | void UpdateAudioBitrate(); |
| 437 | void UpdateSignalBars(); |
| 438 | void UpdateQueuedPackets(); |
| 439 | void SendNopPacket(); |
| 440 | void TickJitterBufferAndCongestionControl(); |
| 441 | void ResetUdpAvailability(); |
| 442 | std::string GetPacketTypeString(PktType type); |
| 443 | void SetupOutgoingVideoStream(); |
| 444 | bool WasOutgoingPacketAcknowledged(std::uint32_t seq); |
| 445 | RecentOutgoingPacket* GetRecentOutgoingPacket(std::uint32_t seq); |
| 446 | void NetworkPacketReceived(std::shared_ptr<NetworkPacket> packet); |
| 447 | void TrySendQueuedPackets(); |
| 448 |
| 449 | TrafficStats m_stats = |
| 450 | { |
| 451 | .bytesSentWifi = 0, |
| 452 | .bytesRecvdWifi = 0, |
| 453 | .bytesSentMobile = 0, |
| 454 | .bytesRecvdMobile = 0 |
| 455 | }; |
| 456 | Config m_config; |
| 457 |
| 458 | State m_state = State::WAIT_INIT; |
| 459 | NetType m_networkType = NetType::UNKNOWN; |
| 460 |
| 461 | MessageThread m_messageThread; |
| 462 |
| 463 | std::int64_t m_preferredRelay = 0; |
| 464 | std::int64_t m_peerPreferredRelay = 0; |
| 465 |
| 466 | tgvoip::audio::AudioIO* m_audioIO = nullptr; |
| 467 | tgvoip::audio::AudioInput* m_audioInput = nullptr; |
| 468 | tgvoip::audio::AudioOutput* m_audioOutput = nullptr; |
| 469 | std::string m_currentAudioInput = "default"; |
| 470 | std::string m_currentAudioOutput = "default"; |
| 471 | std::uint32_t m_audioTimestampIn = 0; |
| 472 | std::uint32_t m_audioTimestampOut = 0; |
| 473 |
| 474 | OpusEncoder* m_encoder = nullptr; |
| 475 | EchoCanceller* m_echoCanceller = nullptr; |
| 476 |
| 477 | Thread* m_recvThread = nullptr; |
| 478 | Thread* m_sendThread = nullptr; |
| 479 |
| 480 | double m_stateChangeTime; |
| 481 | double m_publicEndpointsReqTime = 0; |
| 482 | double m_connectionInitTime = 0; |
| 483 | double m_lastRecvPacketTime = 0; |
| 484 | double m_lastUdpPingTime = 0; |
| 485 |
| 486 | CongestionControl* m_congestionControl; |
| 487 |
| 488 | NetworkSocket* m_udpSocket; |
| 489 | NetworkSocket* m_realUdpSocket; |
| 490 | UdpState m_udpConnectivityState = UdpState::UNKNOWN; |
| 491 | int m_udpPingCount = 0; |
| 492 | SocketSelectCanceller* m_selectCanceller; |
| 493 |
| 494 | FILE* m_statsDump = nullptr; |
| 495 |
| 496 | video::VideoSource* m_videoSource = nullptr; |
| 497 | video::VideoRenderer* m_videoRenderer = nullptr; |
| 498 | video::VideoPacketSender* m_videoPacketSender = nullptr; |
| 499 | std::vector<std::uint32_t> m_peerVideoDecoders; |
| 500 |
| 501 | double m_relaySwitchThreshold; |
| 502 | double m_p2pToRelaySwitchThreshold; |
| 503 | double m_relayToP2pSwitchThreshold; |
| 504 | double m_reconnectingTimeout; |
| 505 | double m_rateMaxAcceptableRTT; |
| 506 | double m_rateMaxAcceptableSendLoss; |
| 507 | double m_packetLossToEnableExtraEC; |
| 508 |
| 509 | HistoricBuffer<std::uint8_t, 4, int> m_signalBarsHistory; |
| 510 |
| 511 | std::deque<RecentOutgoingPacket> m_recentOutgoingPackets; |
| 512 | std::deque<std::uint32_t> m_recentIncomingPackets; |
| 513 |
| 514 | std::uint32_t m_packetsReceived = 0; |
| 515 | std::uint32_t m_recvLossCount = 0; |
| 516 | std::uint32_t m_prevSendLossCount = 0; |
| 517 | std::uint32_t m_firstSentPing; |
| 518 |
| 519 | std::list<PendingOutgoingPacket> m_sendQueue; |
| 520 | BlockingQueue<RawPendingOutgoingPacket> m_rawSendQueue; |
| 521 | std::list<QueuedPacket> m_queuedPackets; |
| 522 | std::list<UnacknowledgedExtraData> m_currentExtras; |
| 523 |
| 524 | HistoricBuffer<std::uint32_t, 5> m_unsentStreamPacketsHistory; |
| 525 | HistoricBuffer<std::uint32_t, 10, double> m_sendLossCountHistory; |
| 526 | HistoricBuffer<double, 32> m_RTTHistory; |
| 527 | std::unordered_map<ExtraType, std::uint64_t> m_lastReceivedExtrasByType; |
| 528 |
| 529 | BufferPool<1024, 32> m_outgoingAudioBufferPool; |
| 530 |
| 531 | std::vector<std::shared_ptr<Stream>> m_outgoingStreams; |
| 532 | std::vector<std::shared_ptr<Stream>> m_incomingStreams; |
| 533 |
| 534 | std::deque<Buffer> m_ecAudioPackets; |
| 535 | std::deque<DebugLoggedPacket> m_debugLoggedPackets; |
| 536 |
| 537 | effects::Volume m_outputVolume; |
| 538 | effects::Volume m_inputVolume; |
| 539 |
| 540 | std::string m_activeNetItfName = ""; |
| 541 |
| 542 | std::string m_proxyAddress = ""; |
| 543 | std::uint16_t m_proxyPort = 0; |
| 544 | std::string m_proxyUsername = ""; |
| 545 | std::string m_proxyPassword = ""; |
| 546 | std::string m_lastTestedProxyServer = ""; |
| 547 | NetworkAddress m_resolvedProxyAddress = NetworkAddress::Empty(); |
| 548 | NetworkAddress m_myIPv6 = NetworkAddress::Empty(); |
| 549 |
| 550 | #if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) |
| 551 | std::function<void(std::int16_t*, std::size_t)> m_audioInputDataCallback; |
| 552 | std::function<void(std::int16_t*, std::size_t)> m_audioOutputDataCallback; |
| 553 | std::function<void(std::int16_t*, std::size_t)> m_audioPreprocDataCallback; |
| 554 | ::OpusDecoder* m_preprocDecoder = nullptr; |
| 555 | std::int16_t m_preprocBuffer[4096]; |
| 556 | #endif |
| 557 |
| 558 | Callbacks m_callbacks = |
| 559 | { |
| 560 | .connectionStateChanged = nullptr, |
| 561 | .signalBarCountChanged = nullptr, |
| 562 | .groupCallKeySent = nullptr, |
| 563 | .groupCallKeyReceived = nullptr, |
| 564 | .upgradeToGroupCallRequested = nullptr |
| 565 | }; |
| 566 |
| 567 | // Locked whenever the endpoints vector is modified (but not endpoints themselves) and whenever iterated outside of messageThread. |
| 568 | // After the call is started, only messageThread is allowed to modify the endpoints vector. |
| 569 | Mutex m_endpointsMutex; |
| 570 |
| 571 | // Locked while audio i/o is being initialized and deinitialized so as to allow it to fully initialize before deinitialization begins. |
| 572 | Mutex m_audioIOMutex; |
| 573 |
| 574 | ProtocolInfo m_protocolInfo = |
| 575 | { |
| 576 | .version = 0, |
| 577 | .maxVideoResolution = InitVideoRes::NONE, |
| 578 | .videoDecoders = {}, |
| 579 | .videoCaptureSupported = false, |
| 580 | .videoDisplaySupported = false, |
| 581 | .callUpgradeSupported = false |
| 582 | }; |
| 583 |
| 584 | std::unordered_map<std::int64_t, Endpoint> m_endpoints; |
| 585 | std::int64_t m_currentEndpoint = 0; |
| 586 |
| 587 | std::atomic<std::uint32_t> m_seq{1}; |
| 588 | std::uint32_t m_lastRemoteSeq = 0; |
| 589 | std::uint32_t m_lastRemoteAckSeq = 0; |
| 590 | std::uint32_t m_lastSentSeq = 0; |
| 591 |
| 592 | int m_dontSendPackets = 0; |
| 593 | Error m_lastError; |
| 594 | std::uint32_t m_maxBitrate; |
| 595 | std::int32_t m_peerVersion = 0; |
| 596 | std::uint32_t m_peerCapabilities = 0; |
| 597 | Proxy m_proxyProtocol = Proxy::NONE; |
| 598 | std::int32_t m_connectionMaxLayer = 0; |
| 599 | std::atomic<std::uint32_t> m_unsentStreamPackets; |
| 600 |
| 601 | int m_echoCancellationStrength = 1; |
| 602 | int m_extraEcLevel = 0; |
| 603 | int m_publicEndpointsReqCount = 0; |
| 604 |
| 605 | std::uint32_t m_initTimeoutID = MessageThread::INVALID_ID; |
| 606 | std::uint32_t m_udpPingTimeoutID = MessageThread::INVALID_ID; |
| 607 |
| 608 | std::uint32_t m_lastReceivedVideoFrameNumber = std::numeric_limits<std::uint32_t>::max(); |
| 609 | std::uint32_t m_sendLosses = 0; |
| 610 | std::uint32_t m_unacknowledgedIncomingPacketCount = 0; |
| 611 |
| 612 | /*** server config values ***/ |
| 613 | std::uint32_t m_maxAudioBitrate; |
| 614 | std::uint32_t m_maxAudioBitrateEDGE; |
| 615 | std::uint32_t m_maxAudioBitrateGPRS; |
| 616 | std::uint32_t m_maxAudioBitrateSaving; |
| 617 | std::uint32_t m_initAudioBitrate; |
| 618 | std::uint32_t m_initAudioBitrateEDGE; |
| 619 | std::uint32_t m_initAudioBitrateGPRS; |
| 620 | std::uint32_t m_initAudioBitrateSaving; |
| 621 | std::uint32_t m_minAudioBitrate; |
| 622 | std::uint32_t m_audioBitrateStepIncr; |
| 623 | std::uint32_t m_audioBitrateStepDecr; |
| 624 |
| 625 | std::uint32_t m_maxUnsentStreamPackets; |
| 626 | std::uint32_t m_unackNopThreshold; |
| 627 |
| 628 | std::uint32_t m_needRateFlags; |
| 629 |
| 630 | std::uint8_t m_encryptionKey[256]; |
| 631 | std::uint8_t m_keyFingerprint[8]; |
| 632 | std::uint8_t m_callID[16]; |
| 633 |
| 634 | std::atomic<bool> m_runReceiver{false}; |
| 635 | std::atomic<bool> m_stopping{false}; |
| 636 | bool m_audioOutStarted = false; |
| 637 | bool m_waitingForAcks = false; |
| 638 | bool m_micMuted = false; |
| 639 | bool m_waitingForRelayPeerInfo = false; |
| 640 | bool m_allowP2p = true; |
| 641 | bool m_dataSavingMode = false; |
| 642 | bool m_dataSavingRequestedByPeer = false; |
| 643 | bool m_receivedInit = false; |
| 644 | bool m_receivedInitAck = false; |
| 645 | bool m_isOutgoing; |
| 646 | bool m_useTCP = false; |
| 647 | bool m_useUDP = true; |
| 648 | bool m_didAddTcpRelays = false; |
| 649 | bool m_audioStarted = false; |
| 650 | bool m_didReceiveGroupCallKey = false; |
| 651 | bool m_didReceiveGroupCallKeyAck = false; |
| 652 | bool m_didSendGroupCallKey = false; |
| 653 | bool m_didSendUpgradeRequest = false; |
| 654 | bool m_didInvokeUpgradeCallback = false; |
| 655 | bool m_useMTProto2 = false; |
| 656 | bool m_setCurrentEndpointToTCP = false; |
| 657 | bool m_useIPv6 = false; |
| 658 | bool m_peerIPv6Available = false; |
| 659 | bool m_didAddIPv6Relays = false; |
| 660 | bool m_didSendIPv6Endpoint = false; |
| 661 | bool m_shittyInternetMode = false; |
| 662 | bool m_wasEstablished = false; |
| 663 | bool m_receivedFirstStreamPacket = false; |
| 664 | bool m_needReInitUdpProxy = true; |
| 665 | bool m_needRate = false; |
| 666 |
| 667 | #if defined(__APPLE__) && defined(TARGET_OS_OSX) |
| 668 | bool macAudioDuckingEnabled = true; |
| 669 | #endif |
| 670 |
| 671 | /*** debug report problems ***/ |
| 672 | bool m_wasReconnecting = false; |
| 673 | bool m_wasExtraEC = false; |
| 674 | bool m_wasEncoderLaggy = false; |
| 675 | bool m_wasNetworkHandover = false; |
| 676 |
| 677 | /*** persistable state values ***/ |
| 678 | bool m_proxySupportsUDP = true; |
| 679 | bool m_proxySupportsTCP = true; |
| 680 | }; |
| 681 |
| 682 | } // namespace tgvoip |
| 683 |
| 684 | #endif // VOIPCONTROLLER_H |
| 685 |