TgVoip.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef __TGVOIP_H
  2. #define __TGVOIP_H
  3. #include <functional>
  4. #include <vector>
  5. #include <string>
  6. #include <memory>
  7. struct TgVoipProxy {
  8. std::string host;
  9. uint16_t port = 0;
  10. std::string login;
  11. std::string password;
  12. };
  13. enum class TgVoipEndpointType {
  14. Inet,
  15. Lan,
  16. UdpRelay,
  17. TcpRelay
  18. };
  19. struct TgVoipEdpointHost {
  20. std::string ipv4;
  21. std::string ipv6;
  22. };
  23. struct TgVoipEndpoint {
  24. int64_t endpointId = 0;
  25. TgVoipEdpointHost host;
  26. uint16_t port = 0;
  27. TgVoipEndpointType type = TgVoipEndpointType();
  28. unsigned char peerTag[16] = { 0 };
  29. };
  30. enum class TgVoipNetworkType {
  31. Unknown,
  32. Gprs,
  33. Edge,
  34. ThirdGeneration,
  35. Hspa,
  36. Lte,
  37. WiFi,
  38. Ethernet,
  39. OtherHighSpeed,
  40. OtherLowSpeed,
  41. OtherMobile,
  42. Dialup
  43. };
  44. enum class TgVoipDataSaving {
  45. Never,
  46. Mobile,
  47. Always
  48. };
  49. struct TgVoipPersistentState {
  50. std::vector<uint8_t> value;
  51. };
  52. #ifdef TGVOIP_USE_CUSTOM_CRYPTO
  53. struct TgVoipCrypto {
  54. void (*rand_bytes)(uint8_t* buffer, size_t length) = nullptr;
  55. void (*sha1)(uint8_t* msg, size_t length, uint8_t* output) = nullptr;
  56. void (*sha256)(uint8_t* msg, size_t length, uint8_t* output) = nullptr;
  57. void (*aes_ige_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv) = nullptr;
  58. void (*aes_ige_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv) = nullptr;
  59. void (*aes_ctr_encrypt)(uint8_t* inout, size_t length, uint8_t* key, uint8_t* iv, uint8_t* ecount, uint32_t* num) = nullptr;
  60. void (*aes_cbc_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv) = nullptr;
  61. void (*aes_cbc_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv) = nullptr;
  62. };
  63. #endif
  64. struct TgVoipConfig {
  65. double initializationTimeout = 0.;
  66. double receiveTimeout = 0.;
  67. TgVoipDataSaving dataSaving = TgVoipDataSaving();
  68. bool enableP2P = false;
  69. bool enableAEC = false;
  70. bool enableNS = false;
  71. bool enableAGC = false;
  72. bool enableVolumeControl = false;
  73. bool enableCallUpgrade = false;
  74. #ifndef _WIN32
  75. std::string logPath;
  76. #else
  77. std::wstring logPath;
  78. #endif
  79. int maxApiLayer = 0;
  80. };
  81. struct TgVoipEncryptionKey {
  82. std::vector<uint8_t> value;
  83. bool isOutgoing = false;
  84. };
  85. enum class TgVoipState {
  86. WaitInit,
  87. WaitInitAck,
  88. Established,
  89. Failed,
  90. Reconnecting
  91. };
  92. struct TgVoipTrafficStats {
  93. uint64_t bytesSentWifi = 0;
  94. uint64_t bytesReceivedWifi = 0;
  95. uint64_t bytesSentMobile = 0;
  96. uint64_t bytesReceivedMobile = 0;
  97. };
  98. struct TgVoipFinalState {
  99. TgVoipPersistentState persistentState;
  100. std::string debugLog;
  101. TgVoipTrafficStats trafficStats;
  102. bool isRatingSuggested = false;
  103. };
  104. struct TgVoipAudioDataCallbacks {
  105. std::function<void(int16_t*, size_t)> input;
  106. std::function<void(int16_t*, size_t)> output;
  107. std::function<void(int16_t*, size_t)> preprocessed;
  108. };
  109. class TgVoip {
  110. protected:
  111. TgVoip() = default;
  112. public:
  113. static void setLoggingFunction(std::function<void(std::string const &)> loggingFunction);
  114. static void setGlobalServerConfig(std::string const &serverConfig);
  115. static int getConnectionMaxLayer();
  116. static std::string getVersion();
  117. static std::unique_ptr<TgVoip> makeInstance(
  118. TgVoipConfig const &config,
  119. TgVoipPersistentState const &persistentState,
  120. std::vector<TgVoipEndpoint> const &endpoints,
  121. TgVoipProxy const *proxy,
  122. TgVoipNetworkType initialNetworkType,
  123. TgVoipEncryptionKey const &encryptionKey
  124. #ifdef TGVOIP_USE_CUSTOM_CRYPTO
  125. ,
  126. TgVoipCrypto const &crypto
  127. #endif
  128. #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
  129. ,
  130. TgVoipAudioDataCallbacks const &audioDataCallbacks
  131. #endif
  132. );
  133. virtual ~TgVoip();
  134. virtual void setNetworkType(TgVoipNetworkType networkType) = 0;
  135. virtual void setMuteMicrophone(bool muteMicrophone) = 0;
  136. virtual void setAudioOutputGainControlEnabled(bool enabled) = 0;
  137. virtual void setEchoCancellationStrength(int strength) = 0;
  138. virtual void setAudioInputDevice(std::string id) = 0;
  139. virtual void setAudioOutputDevice(std::string id) = 0;
  140. virtual void setInputVolume(float level) = 0;
  141. virtual void setOutputVolume(float level) = 0;
  142. virtual void setAudioOutputDuckingEnabled(bool enabled) = 0;
  143. virtual std::string getLastError() = 0;
  144. virtual std::string getDebugInfo() = 0;
  145. virtual int64_t getPreferredRelayId() = 0;
  146. virtual TgVoipTrafficStats getTrafficStats() = 0;
  147. virtual TgVoipPersistentState getPersistentState() = 0;
  148. virtual void setOnStateUpdated(std::function<void(TgVoipState)> onStateUpdated) = 0;
  149. virtual void setOnSignalBarsUpdated(std::function<void(int)> onSignalBarsUpdated) = 0;
  150. virtual TgVoipFinalState stop() = 0;
  151. };
  152. #endif