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