| #include <robuffer.h> |
| 6 | #include <vector> |
| 7 | #include <windows.storage.streams.h> |
| 8 | #include <wrl.h> |
| 9 | #include <wrl/implements.h> |
| 10 |
| 11 | using namespace Platform; |
| 12 |
| 13 | #define STACK_ARRAY(TYPE, LEN) \ |
| 14 | static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE))) |
| 15 |
| 16 | inline std::wstring ToUtf16(const char* utf8, std::size_t len) |
| 17 | { |
| 18 | int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 19 | nullptr, 0); |
| 20 | wchar_t* ws = STACK_ARRAY(wchar_t, len16); |
| 21 | ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16); |
| 22 | return std::wstring(ws, static_cast<std::size_t>(len16)); |
| 23 | } |
| 24 |
| 25 | inline std::wstring ToUtf16(const std::string& str) |
| 26 | { |
| 27 | return ToUtf16(str.data(), str.length()); |
| 28 | } |
| 29 |
| 30 | inline std::string ToUtf8(const wchar_t* wide, std::size_t len) |
| 31 | { |
| 32 | int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 33 | nullptr, 0, nullptr, nullptr); |
| 34 | char* ns = STACK_ARRAY(char, len8); |
| 35 | ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8, |
| 36 | nullptr, nullptr); |
| 37 | return std::string(ns, len8); |
| 38 | } |
| 39 |
| 40 | inline std::string ToUtf8(const wchar_t* wide) |
| 41 | { |
| 42 | return ToUtf8(wide, wcslen(wide)); |
| 43 | } |
| 44 |
| 45 | inline std::string ToUtf8(const std::wstring& wstr) |
| 46 | { |
| 47 | return ToUtf8(wstr.data(), wstr.length()); |
| 48 | } |
| 49 |
| 50 | namespace libtgvoip |
| 51 | { |
| 52 | public |
| 53 | ref class Endpoint sealed |
| 54 | { |
| 55 | public: |
| 56 | property int64 id; |
| 57 | property uint16 port; |
| 58 | property Platform::String ^ ipv4; |
| 59 | property Platform::String ^ ipv6; |
| 60 | property Platform::Array<uint8> ^ peerTag; |
| 61 | }; |
| 62 |
| 63 | public |
| 64 | ref class TrafficStats sealed |
| 65 | { |
| 66 | public: |
| 67 | property std::uint64_t bytesSentWifi; |
| 68 | property std::uint64_t bytesRecvdWifi; |
| 69 | property std::uint64_t bytesSentMobile; |
| 70 | property std::uint64_t bytesRecvdMobile; |
| 71 | }; |
| 72 |
| 73 | public |
| 74 | enum class CallState : int |
| 75 | { |
| 76 | WaitInit = 1, |
| 77 | WaitInitAck, |
| 78 | Established, |
| 79 | Failed |
| 80 | }; |
| 81 |
| 82 | public |
| 83 | enum class Error : int |
| 84 | { |
| 85 | Unknown = 0, |
| 86 | Incompatible, |
| 87 | Timeout, |
| 88 | AudioIO |
| 89 | }; |
| 90 |
| 91 | public |
| 92 | enum class NetworkType : int |
| 93 | { |
| 94 | Unknown = 0, |
| 95 | GPRS, |
| 96 | EDGE, |
| 97 | UMTS, |
| 98 | HSPA, |
| 99 | LTE, |
| 100 | WiFi, |
| 101 | Ethernet, |
| 102 | OtherHighSpeed, |
| 103 | OtherLowSpeed, |
| 104 | Dialup, |
| 105 | OtherMobile |
| 106 | }; |
| 107 |
| 108 | public |
| 109 | enum class DataSavingMode |
| 110 | { |
| 111 | Never = 0, |
| 112 | MobileOnly, |
| 113 | Always |
| 114 | }; |
| 115 |
| 116 | public |
| 117 | enum class ProxyProtocol |
| 118 | { |
| 119 | None = 0, |
| 120 | SOCKS5 |
| 121 | }; |
| 122 |
| 123 | public |
| 124 | ref class VoIPConfig sealed |
| 125 | { |
| 126 | public: |
| 127 | VoIPConfig() |
| 128 | { |
| 129 | logPacketStats = false; |
| 130 | enableVolumeControl = false; |
| 131 | enableVideoSend = false; |
| 132 | enableVideoReceive = false; |
| 133 | } |
| 134 |
| 135 | property double initTimeout; |
| 136 | property double recvTimeout; |
| 137 | property DataSavingMode dataSaving; |
| 138 | property String ^ logFilePath; |
| 139 | property String ^ statsDumpFilePath; |
| 140 |
| 141 | property bool enableAEC; |
| 142 | property bool enableNS; |
| 143 | property bool enableAGC; |
| 144 |
| 145 | property bool enableCallUpgrade; |
| 146 |
| 147 | property bool logPacketStats; |
| 148 | property bool enableVolumeControl; |
| 149 |
| 150 | property bool enableVideoSend; |
| 151 | property bool enableVideoReceive; |
| 152 | }; |
| 153 |
| 154 | ref class VoIPControllerWrapper; |
| 155 | public |
| 156 | delegate void CallStateChangedEventHandler(VoIPControllerWrapper ^ sender, CallState newState); |
| 157 |
| 158 | ref class VoIPControllerWrapper; |
| 159 | public |
| 160 | delegate void SignalBarsChangedEventHandler(VoIPControllerWrapper ^ sender, int newCount); |
| 161 |
| 162 | public |
| 163 | ref class VoIPControllerWrapper sealed |
| 164 | { |
| 165 | public: |
| 166 | VoIPControllerWrapper(); |
| 167 | virtual ~VoIPControllerWrapper(); |
| 168 | void Start(); |
| 169 | void Connect(); |
| 170 | void SetPublicEndpoints(const Platform::Array<Endpoint ^> ^ endpoints, bool allowP2P, std::int32_t connectionMaxLayer); |
| 171 | void SetNetworkType(NetworkType type); |
| 172 | void SetMicMute(bool mute); |
| 173 | void SetEncryptionKey(const Platform::Array<uint8> ^ key, bool isOutgoing); |
| 174 | void SetConfig(VoIPConfig ^ config); |
| 175 | void SetProxy(ProxyProtocol protocol, Platform::String ^ address, std::uint16_t port, Platform::String ^ username, Platform::String ^ password); |
| 176 | int GetSignalBarsCount(); |
| 177 | CallState GetConnectionState(); |
| 178 | TrafficStats ^ GetStats(); |
| 179 | Platform::String ^ GetDebugString(); |
| 180 | Platform::String ^ GetDebugLog(); |
| 181 | Error GetLastError(); |
| 182 | static Platform::String ^ GetVersion(); |
| 183 | int64 GetPreferredRelayID(); |
| 184 | void SetAudioOutputGainControlEnabled(bool enabled); |
| 185 |
| 186 | void SetInputVolume(float level); |
| 187 | void SetOutputVolume(float level); |
| 188 |
| 189 | property String ^ CurrentAudioInput { |
| 190 | String ^ get() { |
| 191 | return ref new String(ToUtf16(controller->GetCurrentAudioInputID()).data()); |
| 192 | } void set(String ^ value) |
| 193 | { |
| 194 | controller->SetCurrentAudioInput(ToUtf8(value->Data())); |
| 195 | } |
| 196 | } |
| 197 |
| 198 | property String |
| 199 | ^ CurrentAudioOutput { |
| 200 | String ^ get() { |
| 201 | return ref new String(ToUtf16(controller->GetCurrentAudioOutputID()).data()); |
| 202 | } void set(String ^ value) |
| 203 | { |
| 204 | controller->SetCurrentAudioOutput(ToUtf8(value->Data())); |
| 205 | } |
| 206 | } |
| 207 |
| 208 | static std::int32_t |
| 209 | GetConnectionMaxLayer(); |
| 210 | static void UpdateServerConfig(Platform::String ^ json); |
| 211 | static void SwitchSpeaker(bool external); |
| 212 | //static Platform::String^ TestAesIge(); |
| 213 |
| 214 | event CallStateChangedEventHandler ^ CallStateChanged; |
| 215 | event SignalBarsChangedEventHandler ^ SignalBarsChanged; |
| 216 |
| 217 | private: |
| 218 | static void OnStateChanged(tgvoip::VoIPController* c, int state); |
| 219 | static void OnSignalBarsChanged(tgvoip::VoIPController* c, int count); |
| 220 | void OnStateChangedInternal(int state); |
| 221 | void OnSignalBarsChangedInternal(int count); |
| 222 | tgvoip::VoIPController* controller; |
| 223 | }; |
| 224 |
| 225 | ref class MicrosoftCryptoImpl |
| 226 | { |
| 227 | public: |
| 228 | static void AesIgeEncrypt(std::uint8_t* in, std::uint8_t* out, std::size_t len, std::uint8_t* key, std::uint8_t* iv); |
| 229 | static void AesIgeDecrypt(std::uint8_t* in, std::uint8_t* out, std::size_t len, std::uint8_t* key, std::uint8_t* iv); |
| 230 | static void AesCtrEncrypt(std::uint8_t* inout, std::size_t len, std::uint8_t* key, std::uint8_t* iv, std::uint8_t* ecount, std::uint32_t* num); |
| 231 | static void SHA1(std::uint8_t* msg, std::size_t len, std::uint8_t* out); |
| 232 | static void SHA256(std::uint8_t* msg, std::size_t len, std::uint8_t* out); |
| 233 | static void RandBytes(std::uint8_t* buffer, std::size_t len); |
| 234 | static void Init(); |
| 235 |
| 236 | private: |
| 237 | static inline void XorInt128(std::uint8_t* a, std::uint8_t* b, std::uint8_t* out); |
| 238 | static void IBufferToPtr(Windows::Storage::Streams::IBuffer ^ buffer, std::size_t len, std::uint8_t* out); |
| 239 | static Windows::Storage::Streams::IBuffer ^ IBufferFromPtr(std::uint8_t* msg, std::size_t len); |
| 240 | /*static Windows::Security::Cryptography::Core::CryptographicHash^ sha1Hash; |
| 241 | static Windows::Security::Cryptography::Core::CryptographicHash^ sha256Hash;*/ |
| 242 | static Windows::Security::Cryptography::Core::HashAlgorithmProvider ^ sha1Provider; |
| 243 | static Windows::Security::Cryptography::Core::HashAlgorithmProvider ^ sha256Provider; |
| 244 | static Windows::Security::Cryptography::Core::SymmetricKeyAlgorithmProvider ^ aesKeyProvider; |
| 245 | }; |
| 246 |
| 247 | class NativeBuffer : public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>, |
| 248 | ABI::Windows::Storage::Streams::IBuffer, |
| 249 | Windows::Storage::Streams::IBufferByteAccess> |
| 250 | { |
| 251 | public: |
| 252 | NativeBuffer(byte* buffer, UINT totalSize) |
| 253 | { |
| 254 | m_length = totalSize; |
| 255 | m_buffer = buffer; |
| 256 | } |
| 257 |
| 258 | virtual ~NativeBuffer() |
| 259 | { |
| 260 | } |
| 261 |
| 262 | STDMETHODIMP RuntimeClassInitialize(byte* buffer, UINT totalSize) |
| 263 | { |
| 264 | m_length = totalSize; |
| 265 | m_buffer = buffer; |
| 266 | return S_OK; |
| 267 | } |
| 268 |
| 269 | STDMETHODIMP Buffer(byte** value) |
| 270 | { |
| 271 | *value = m_buffer; |
| 272 | return S_OK; |
| 273 | } |
| 274 |
| 275 | STDMETHODIMP get_Capacity(UINT32* value) |
| 276 | { |
| 277 | *value = m_length; |
| 278 | return S_OK; |
| 279 | } |
| 280 |
| 281 | STDMETHODIMP get_Length(UINT32* value) |
| 282 | { |
| 283 | *value = m_length; |
| 284 | return S_OK; |
| 285 | } |
| 286 |
| 287 | STDMETHODIMP put_Length(UINT32 value) |
| 288 | { |
| 289 | m_length = value; |
| 290 | return S_OK; |
| 291 | } |
| 292 |
| 293 | private: |
| 294 | UINT32 m_length; |
| 295 | byte* m_buffer; |
| 296 | }; |
| 297 | } |
| 298 |