2020-03-10 19:36:27 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
2019-12-27 12:19:21 +01:00
|
|
|
#include "TgVoip.h"
|
|
|
|
|
|
|
|
#include "VoIPController.h"
|
|
|
|
#include "VoIPServerConfig.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#ifndef TGVOIP_USE_CUSTOM_CRYPTO
|
2020-01-22 12:43:51 +01:00
|
|
|
extern "C"
|
|
|
|
{
|
2019-12-27 12:19:21 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/modes.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_aes_ige_encrypt(uint8_t *in, uint8_t *out, size_t length, uint8_t *key, uint8_t *iv)
|
|
|
|
{
|
|
|
|
AES_KEY akey;
|
|
|
|
AES_set_encrypt_key(key, 32 * 8, &akey);
|
|
|
|
AES_ige_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_aes_ige_decrypt(uint8_t *in, uint8_t *out, size_t length, uint8_t *key, uint8_t *iv)
|
|
|
|
{
|
|
|
|
AES_KEY akey;
|
|
|
|
AES_set_decrypt_key(key, 32 * 8, &akey);
|
|
|
|
AES_ige_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_rand_bytes(uint8_t *buffer, size_t len)
|
|
|
|
{
|
|
|
|
RAND_bytes(buffer, len);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_sha1(uint8_t *msg, size_t len, uint8_t *output)
|
|
|
|
{
|
|
|
|
SHA1(msg, len, output);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_sha256(uint8_t *msg, size_t len, uint8_t *output)
|
|
|
|
{
|
|
|
|
SHA256(msg, len, output);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_aes_ctr_encrypt(uint8_t *inout, size_t length, uint8_t *key, uint8_t *iv, uint8_t *ecount, uint32_t *num)
|
|
|
|
{
|
|
|
|
AES_KEY akey;
|
|
|
|
AES_set_encrypt_key(key, 32 * 8, &akey);
|
|
|
|
CRYPTO_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num, (block128_f)AES_encrypt);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_aes_cbc_encrypt(uint8_t *in, uint8_t *out, size_t length, uint8_t *key, uint8_t *iv)
|
|
|
|
{
|
|
|
|
AES_KEY akey;
|
|
|
|
AES_set_encrypt_key(key, 256, &akey);
|
|
|
|
AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void tgvoip_openssl_aes_cbc_decrypt(uint8_t *in, uint8_t *out, size_t length, uint8_t *key, uint8_t *iv)
|
|
|
|
{
|
|
|
|
AES_KEY akey;
|
|
|
|
AES_set_decrypt_key(key, 256, &akey);
|
|
|
|
AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
|
2019-12-27 12:19:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
tgvoip::CryptoFunctions tgvoip::VoIPController::crypto = {
|
2019-12-27 12:19:21 +01:00
|
|
|
tgvoip_openssl_rand_bytes,
|
|
|
|
tgvoip_openssl_sha1,
|
|
|
|
tgvoip_openssl_sha256,
|
|
|
|
tgvoip_openssl_aes_ige_encrypt,
|
|
|
|
tgvoip_openssl_aes_ige_decrypt,
|
|
|
|
tgvoip_openssl_aes_ctr_encrypt,
|
|
|
|
tgvoip_openssl_aes_cbc_encrypt,
|
2020-01-22 12:43:51 +01:00
|
|
|
tgvoip_openssl_aes_cbc_decrypt};
|
2019-12-27 12:19:21 +01:00
|
|
|
#endif
|
2019-12-24 19:57:22 +01:00
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
class TgVoipImpl : public TgVoip
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
public:
|
|
|
|
TgVoipImpl(
|
|
|
|
std::vector<TgVoipEndpoint> const &endpoints,
|
|
|
|
TgVoipPersistentState const &persistentState,
|
|
|
|
std::unique_ptr<TgVoipProxy> const &proxy,
|
|
|
|
TgVoipConfig const &config,
|
|
|
|
TgVoipEncryptionKey const &encryptionKey,
|
|
|
|
TgVoipNetworkType initialNetworkType
|
2019-12-27 12:19:21 +01:00
|
|
|
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
|
|
|
|
,
|
|
|
|
TgVoipCrypto const &crypto
|
|
|
|
#endif
|
|
|
|
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
|
2019-12-24 19:57:22 +01:00
|
|
|
,
|
|
|
|
TgVoipAudioDataCallbacks const &audioDataCallbacks
|
|
|
|
#endif
|
2020-01-22 12:43:51 +01:00
|
|
|
)
|
|
|
|
{
|
2019-12-27 12:19:21 +01:00
|
|
|
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
|
2019-12-24 19:57:22 +01:00
|
|
|
tgvoip::VoIPController::crypto.sha1 = crypto.sha1;
|
|
|
|
tgvoip::VoIPController::crypto.sha256 = crypto.sha256;
|
|
|
|
tgvoip::VoIPController::crypto.rand_bytes = crypto.rand_bytes;
|
|
|
|
tgvoip::VoIPController::crypto.aes_ige_encrypt = crypto.aes_ige_encrypt;
|
|
|
|
tgvoip::VoIPController::crypto.aes_ige_decrypt = crypto.aes_ige_decrypt;
|
|
|
|
tgvoip::VoIPController::crypto.aes_ctr_encrypt = crypto.aes_ctr_encrypt;
|
2019-12-27 12:19:21 +01:00
|
|
|
#endif
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_ = new tgvoip::VoIPController();
|
|
|
|
controller_->implData = this;
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-27 12:19:21 +01:00
|
|
|
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetAudioDataCallbacks(audioDataCallbacks.input, audioDataCallbacks.output, audioDataCallbacks.preprocessed);
|
|
|
|
#endif
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetPersistentState(persistentState.value);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
if (proxy != nullptr)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetProxy(tgvoip::PROXY_SOCKS5, proxy->host, proxy->port, proxy->login, proxy->password);
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
auto callbacks = tgvoip::VoIPController::Callbacks();
|
|
|
|
callbacks.connectionStateChanged = &TgVoipImpl::controllerStateCallback;
|
2020-03-10 16:30:57 +01:00
|
|
|
callbacks.groupCallKeyReceived = nullptr;
|
|
|
|
callbacks.groupCallKeySent = nullptr;
|
2019-12-24 19:57:22 +01:00
|
|
|
callbacks.signalBarCountChanged = &TgVoipImpl::signalBarsCallback;
|
2020-03-10 16:30:57 +01:00
|
|
|
callbacks.upgradeToGroupCallRequested = nullptr;
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetCallbacks(callbacks);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
std::vector<tgvoip::Endpoint> mappedEndpoints;
|
2020-01-22 12:43:51 +01:00
|
|
|
for (auto endpoint : endpoints)
|
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
tgvoip::Endpoint::Type mappedType;
|
2020-01-22 12:43:51 +01:00
|
|
|
switch (endpoint.type)
|
|
|
|
{
|
|
|
|
case TgVoipEndpointType::UdpRelay:
|
|
|
|
mappedType = tgvoip::Endpoint::Type::UDP_RELAY;
|
|
|
|
break;
|
|
|
|
case TgVoipEndpointType::Lan:
|
|
|
|
mappedType = tgvoip::Endpoint::Type::UDP_P2P_LAN;
|
|
|
|
break;
|
|
|
|
case TgVoipEndpointType::Inet:
|
|
|
|
mappedType = tgvoip::Endpoint::Type::UDP_P2P_INET;
|
|
|
|
break;
|
|
|
|
case TgVoipEndpointType::TcpRelay:
|
|
|
|
mappedType = tgvoip::Endpoint::Type::TCP_RELAY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mappedType = tgvoip::Endpoint::Type::UDP_RELAY;
|
|
|
|
break;
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 16:30:57 +01:00
|
|
|
tgvoip::IPv4Address address(endpoint.host.ipv4);
|
|
|
|
tgvoip::IPv6Address addressv6(endpoint.host.ipv6);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
mappedEndpoints.emplace_back(endpoint.endpointId, endpoint.port, address, addressv6, mappedType, endpoint.peerTag);
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
int mappedDataSaving;
|
2020-01-22 12:43:51 +01:00
|
|
|
switch (config.dataSaving)
|
|
|
|
{
|
|
|
|
case TgVoipDataSaving::Mobile:
|
|
|
|
mappedDataSaving = tgvoip::DATA_SAVING_MOBILE;
|
|
|
|
break;
|
|
|
|
case TgVoipDataSaving::Always:
|
|
|
|
mappedDataSaving = tgvoip::DATA_SAVING_ALWAYS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mappedDataSaving = tgvoip::DATA_SAVING_NEVER;
|
|
|
|
break;
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-01-28 12:34:24 +01:00
|
|
|
tgvoip::VoIPController::Config mappedConfig(
|
2019-12-24 19:57:22 +01:00
|
|
|
config.initializationTimeout,
|
|
|
|
config.receiveTimeout,
|
|
|
|
mappedDataSaving,
|
|
|
|
config.enableAEC,
|
|
|
|
config.enableNS,
|
|
|
|
config.enableAGC,
|
2020-01-22 12:43:51 +01:00
|
|
|
config.enableCallUpgrade);
|
2019-12-24 19:57:22 +01:00
|
|
|
mappedConfig.logFilePath = config.logPath;
|
2020-03-10 16:30:57 +01:00
|
|
|
mappedConfig.statsDumpFilePath = {};
|
2019-12-24 19:57:22 +01:00
|
|
|
|
|
|
|
controller_->SetConfig(mappedConfig);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
setNetworkType(initialNetworkType);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-01-23 18:06:11 +01:00
|
|
|
controller_->SetEncryptionKey(encryptionKey.value, encryptionKey.isOutgoing);
|
2020-03-28 17:05:24 +01:00
|
|
|
controller_->SetRemoteEndpoints(mappedEndpoints, config.enableP2P, config.maxApiLayer);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->Start();
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->Connect();
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
~TgVoipImpl()
|
|
|
|
{
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
void setOnStateUpdated(std::function<void(TgVoipState)> onStateUpdated)
|
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
std::lock_guard<std::mutex> lock(m_onStateUpdated);
|
2019-12-24 19:57:22 +01:00
|
|
|
onStateUpdated_ = onStateUpdated;
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
void setOnSignalBarsUpdated(std::function<void(int)> onSignalBarsUpdated)
|
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
std::lock_guard<std::mutex> lock(m_onSignalBarsUpdated);
|
2019-12-24 19:57:22 +01:00
|
|
|
onSignalBarsUpdated_ = onSignalBarsUpdated;
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
void setNetworkType(TgVoipNetworkType networkType) override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2020-03-10 16:30:57 +01:00
|
|
|
int mappedType;
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
switch (networkType)
|
|
|
|
{
|
|
|
|
case TgVoipNetworkType::Unknown:
|
|
|
|
mappedType = tgvoip::NET_TYPE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Gprs:
|
|
|
|
mappedType = tgvoip::NET_TYPE_GPRS;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Edge:
|
|
|
|
mappedType = tgvoip::NET_TYPE_EDGE;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::ThirdGeneration:
|
|
|
|
mappedType = tgvoip::NET_TYPE_3G;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Hspa:
|
|
|
|
mappedType = tgvoip::NET_TYPE_HSPA;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Lte:
|
|
|
|
mappedType = tgvoip::NET_TYPE_LTE;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::WiFi:
|
|
|
|
mappedType = tgvoip::NET_TYPE_WIFI;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Ethernet:
|
|
|
|
mappedType = tgvoip::NET_TYPE_ETHERNET;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::OtherHighSpeed:
|
|
|
|
mappedType = tgvoip::NET_TYPE_OTHER_HIGH_SPEED;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::OtherLowSpeed:
|
|
|
|
mappedType = tgvoip::NET_TYPE_OTHER_LOW_SPEED;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::OtherMobile:
|
|
|
|
mappedType = tgvoip::NET_TYPE_OTHER_MOBILE;
|
|
|
|
break;
|
|
|
|
case TgVoipNetworkType::Dialup:
|
|
|
|
mappedType = tgvoip::NET_TYPE_DIALUP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mappedType = tgvoip::NET_TYPE_UNKNOWN;
|
|
|
|
break;
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetNetworkType(mappedType);
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:30:57 +01:00
|
|
|
void setMuteMicrophone(bool muteMicrophone) override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
controller_->SetMicMute(muteMicrophone);
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
void setAudioOutputGainControlEnabled(bool enabled) override
|
|
|
|
{
|
|
|
|
controller_->SetAudioOutputGainControlEnabled(enabled);
|
2020-03-10 16:30:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
void setEchoCancellationStrength(int strength) override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
controller_->SetEchoCancellationStrength(strength);
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
std::string getLastError() override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
switch (controller_->GetLastError())
|
|
|
|
{
|
|
|
|
case tgvoip::ERROR_INCOMPATIBLE:
|
|
|
|
return "ERROR_INCOMPATIBLE";
|
|
|
|
case tgvoip::ERROR_TIMEOUT:
|
|
|
|
return "ERROR_TIMEOUT";
|
|
|
|
case tgvoip::ERROR_AUDIO_IO:
|
|
|
|
return "ERROR_AUDIO_IO";
|
|
|
|
case tgvoip::ERROR_PROXY:
|
|
|
|
return "ERROR_PROXY";
|
|
|
|
default:
|
|
|
|
return "ERROR_UNKNOWN";
|
|
|
|
}
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 16:30:57 +01:00
|
|
|
std::string getDebugInfo() override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
return controller_->GetDebugString();
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
int64_t getPreferredRelayId() override
|
|
|
|
{
|
2020-03-10 16:30:57 +01:00
|
|
|
return controller_->GetPreferredRelayID();
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
TgVoipTrafficStats getTrafficStats() override
|
2020-01-22 12:43:51 +01:00
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
tgvoip::VoIPController::TrafficStats stats;
|
|
|
|
controller_->GetStats(&stats);
|
2020-03-10 19:36:27 +01:00
|
|
|
return {
|
2019-12-24 19:57:22 +01:00
|
|
|
.bytesSentWifi = stats.bytesSentWifi,
|
|
|
|
.bytesReceivedWifi = stats.bytesRecvdWifi,
|
|
|
|
.bytesSentMobile = stats.bytesSentMobile,
|
2020-01-22 12:43:51 +01:00
|
|
|
.bytesReceivedMobile = stats.bytesRecvdMobile};
|
2020-03-10 19:36:27 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
TgVoipPersistentState getPersistentState() override
|
|
|
|
{
|
|
|
|
return {controller_->GetPersistentState()};
|
|
|
|
}
|
|
|
|
|
|
|
|
TgVoipFinalState stop() override
|
|
|
|
{
|
|
|
|
controller_->Stop();
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
TgVoipFinalState finalState = {
|
2020-03-10 19:36:27 +01:00
|
|
|
.persistentState = getPersistentState(),
|
|
|
|
.debugLog = controller_->GetDebugLog(),
|
|
|
|
.trafficStats = getTrafficStats(),
|
2020-01-22 12:43:51 +01:00
|
|
|
.isRatingSuggested = controller_->NeedRate()};
|
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
delete controller_;
|
2020-03-10 16:30:57 +01:00
|
|
|
controller_ = nullptr;
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
return finalState;
|
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
static void controllerStateCallback(tgvoip::VoIPController *controller, int state)
|
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
TgVoipImpl *self = reinterpret_cast<TgVoipImpl *>(controller->implData);
|
|
|
|
std::lock_guard<std::mutex> lock(self->m_onStateUpdated);
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
if (self->onStateUpdated_)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
TgVoipState mappedState;
|
2020-01-22 12:43:51 +01:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case tgvoip::STATE_WAIT_INIT:
|
|
|
|
mappedState = TgVoipState::WaitInit;
|
|
|
|
break;
|
|
|
|
case tgvoip::STATE_WAIT_INIT_ACK:
|
|
|
|
mappedState = TgVoipState::WaitInitAck;
|
|
|
|
break;
|
|
|
|
case tgvoip::STATE_ESTABLISHED:
|
|
|
|
mappedState = TgVoipState::Estabilished;
|
|
|
|
break;
|
|
|
|
case tgvoip::STATE_FAILED:
|
|
|
|
mappedState = TgVoipState::Failed;
|
|
|
|
break;
|
|
|
|
case tgvoip::STATE_RECONNECTING:
|
|
|
|
mappedState = TgVoipState::Reconnecting;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mappedState = TgVoipState::Estabilished;
|
|
|
|
break;
|
2019-12-24 19:57:22 +01:00
|
|
|
}
|
2020-01-22 12:43:51 +01:00
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
self->onStateUpdated_(mappedState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
static void signalBarsCallback(tgvoip::VoIPController *controller, int signalBars)
|
|
|
|
{
|
2020-03-10 19:36:27 +01:00
|
|
|
TgVoipImpl *self = reinterpret_cast<TgVoipImpl *>(controller->implData);
|
|
|
|
std::lock_guard<std::mutex> lock(self->m_onSignalBarsUpdated);
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
if (self->onSignalBarsUpdated_)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
self->onSignalBarsUpdated_(signalBars);
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 19:36:27 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
tgvoip::VoIPController *controller_;
|
|
|
|
std::function<void(TgVoipState)> onStateUpdated_;
|
|
|
|
std::function<void(int)> onSignalBarsUpdated_;
|
|
|
|
std::mutex m_onStateUpdated, m_onSignalBarsUpdated;
|
2019-12-24 19:57:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::function<void(std::string const &)> globalLoggingFunction;
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void __tgvoip_call_tglog(const char *format, ...)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
va_list vaArgs;
|
|
|
|
va_start(vaArgs, format);
|
|
|
|
|
|
|
|
va_list vaCopy;
|
|
|
|
va_copy(vaCopy, vaArgs);
|
2020-03-10 19:36:27 +01:00
|
|
|
const int length = std::vsnprintf(nullptr, 0, format, vaCopy);
|
2019-12-24 19:57:22 +01:00
|
|
|
va_end(vaCopy);
|
|
|
|
|
|
|
|
std::vector<char> zc(length + 1);
|
|
|
|
std::vsnprintf(zc.data(), zc.size(), format, vaArgs);
|
|
|
|
va_end(vaArgs);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
if (globalLoggingFunction != nullptr)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
globalLoggingFunction(std::string(zc.data(), zc.size()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void TgVoip::setLoggingFunction(std::function<void(std::string const &)> loggingFunction)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
globalLoggingFunction = loggingFunction;
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
void TgVoip::setGlobalServerConfig(const std::string &serverConfig)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
tgvoip::ServerConfig::GetSharedInstance()->Update(serverConfig);
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
int TgVoip::getConnectionMaxLayer()
|
|
|
|
{
|
|
|
|
return tgvoip::VoIPController::GetConnectionMaxLayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TgVoip::getVersion()
|
|
|
|
{
|
|
|
|
return tgvoip::VoIPController::GetVersion();
|
|
|
|
}
|
|
|
|
|
2019-12-24 19:57:22 +01:00
|
|
|
TgVoip *TgVoip::makeInstance(
|
|
|
|
TgVoipConfig const &config,
|
|
|
|
TgVoipPersistentState const &persistentState,
|
|
|
|
std::vector<TgVoipEndpoint> const &endpoints,
|
|
|
|
std::unique_ptr<TgVoipProxy> const &proxy,
|
|
|
|
TgVoipNetworkType initialNetworkType,
|
|
|
|
TgVoipEncryptionKey const &encryptionKey
|
2019-12-27 12:19:21 +01:00
|
|
|
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
|
|
|
|
,
|
|
|
|
TgVoipCrypto const &crypto
|
|
|
|
#endif
|
|
|
|
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
|
2019-12-24 19:57:22 +01:00
|
|
|
,
|
|
|
|
TgVoipAudioDataCallbacks const &audioDataCallbacks
|
|
|
|
#endif
|
2020-01-22 12:43:51 +01:00
|
|
|
)
|
|
|
|
{
|
2019-12-24 19:57:22 +01:00
|
|
|
return new TgVoipImpl(
|
|
|
|
endpoints,
|
|
|
|
persistentState,
|
|
|
|
proxy,
|
|
|
|
config,
|
|
|
|
encryptionKey,
|
|
|
|
initialNetworkType
|
2019-12-27 12:19:21 +01:00
|
|
|
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
|
|
|
|
,
|
|
|
|
crypto
|
|
|
|
#endif
|
|
|
|
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
|
2019-12-24 19:57:22 +01:00
|
|
|
,
|
|
|
|
audioDataCallbacks
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:36:27 +01:00
|
|
|
TgVoip::~TgVoip() = default;
|