diff --git a/TgVoip.cpp b/TgVoip.cpp index 94b40d9..a6bd11a 100755 --- a/TgVoip.cpp +++ b/TgVoip.cpp @@ -1,3 +1,5 @@ +#include + #include "TgVoip.h" #include "VoIPController.h" @@ -73,17 +75,11 @@ tgvoip::CryptoFunctions tgvoip::VoIPController::crypto = { tgvoip_openssl_aes_ctr_encrypt, tgvoip_openssl_aes_cbc_encrypt, tgvoip_openssl_aes_cbc_decrypt}; -#else -tgvoip::CryptoFunctions tgvoip::VoIPController::crypto; // set it yourself upon initialization #endif class TgVoipImpl : public TgVoip { public: - tgvoip::VoIPController *controller_; - std::function onStateUpdated_; - std::function onSignalBarsUpdated_; - TgVoipImpl( std::vector const &endpoints, TgVoipPersistentState const &persistentState, @@ -135,7 +131,7 @@ public: std::vector mappedEndpoints; for (auto endpoint : endpoints) { - tgvoip::Endpoint::Type mappedType = tgvoip::Endpoint::Type::UDP_RELAY; + tgvoip::Endpoint::Type mappedType; switch (endpoint.type) { case TgVoipEndpointType::UdpRelay: @@ -158,10 +154,10 @@ public: tgvoip::IPv4Address address(endpoint.host.ipv4); tgvoip::IPv6Address addressv6(endpoint.host.ipv6); - mappedEndpoints.push_back(tgvoip::Endpoint(endpoint.endpointId, endpoint.port, address, addressv6, mappedType, endpoint.peerTag)); + mappedEndpoints.emplace_back(endpoint.endpointId, endpoint.port, address, addressv6, mappedType, endpoint.peerTag); } - int mappedDataSaving = tgvoip::DATA_SAVING_NEVER; + int mappedDataSaving; switch (config.dataSaving) { case TgVoipDataSaving::Mobile: @@ -198,19 +194,23 @@ public: controller_->Connect(); } - ~TgVoipImpl() override = default; + ~TgVoipImpl() + { + } void setOnStateUpdated(std::function onStateUpdated) { + std::lock_guard lock(m_onStateUpdated); onStateUpdated_ = onStateUpdated; } void setOnSignalBarsUpdated(std::function onSignalBarsUpdated) { + std::lock_guard lock(m_onSignalBarsUpdated); onSignalBarsUpdated_ = onSignalBarsUpdated; } - void setNetworkType(TgVoipNetworkType networkType) + void setNetworkType(TgVoipNetworkType networkType) override { int mappedType; @@ -265,66 +265,67 @@ public: controller_->SetMicMute(muteMicrophone); } - void setEchoCancellationStrength(int strength) override { + void setAudioOutputGainControlEnabled(bool enabled) override + { + controller_->SetAudioOutputGainControlEnabled(enabled); + } + + void setEchoCancellationStrength(int strength) override + { controller_->SetEchoCancellationStrength(strength); } - std::string getLastError() override { - 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"; + std::string getLastError() override + { + 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"; } } - std::string getVersion() - { - return controller_->GetVersion(); - } - - TgVoipPersistentState getPersistentState() - { - std::vector persistentStateValue = controller_->GetPersistentState(); - TgVoipPersistentState persistentState = { - .value = persistentStateValue}; - - return persistentState; - } - std::string getDebugInfo() override { return controller_->GetDebugString(); } - int64_t getPreferredRelayId() override { + int64_t getPreferredRelayId() override + { return controller_->GetPreferredRelayID(); } - TgVoipFinalState stop() + TgVoipTrafficStats getTrafficStats() override { - controller_->Stop(); - - auto debugLog = controller_->GetDebugLog(); - tgvoip::VoIPController::TrafficStats stats; controller_->GetStats(&stats); - - TgVoipTrafficStats trafficStats = { + return { .bytesSentWifi = stats.bytesSentWifi, .bytesReceivedWifi = stats.bytesRecvdWifi, .bytesSentMobile = stats.bytesSentMobile, .bytesReceivedMobile = stats.bytesRecvdMobile}; + } - std::vector persistentStateValue = controller_->GetPersistentState(); - TgVoipPersistentState persistentState = { - .value = persistentStateValue}; + TgVoipPersistentState getPersistentState() override + { + return {controller_->GetPersistentState()}; + } + + TgVoipFinalState stop() override + { + controller_->Stop(); TgVoipFinalState finalState = { - .persistentState = persistentState, - .debugLog = debugLog, - .trafficStats = trafficStats, + .persistentState = getPersistentState(), + .debugLog = controller_->GetDebugLog(), + .trafficStats = getTrafficStats(), .isRatingSuggested = controller_->NeedRate()}; delete controller_; @@ -335,7 +336,9 @@ public: static void controllerStateCallback(tgvoip::VoIPController *controller, int state) { - TgVoipImpl *self = (TgVoipImpl *)controller->implData; + TgVoipImpl *self = reinterpret_cast(controller->implData); + std::lock_guard lock(self->m_onStateUpdated); + if (self->onStateUpdated_) { TgVoipState mappedState; @@ -367,12 +370,20 @@ public: static void signalBarsCallback(tgvoip::VoIPController *controller, int signalBars) { - TgVoipImpl *self = (TgVoipImpl *)controller->implData; + TgVoipImpl *self = reinterpret_cast(controller->implData); + std::lock_guard lock(self->m_onSignalBarsUpdated); + if (self->onSignalBarsUpdated_) { self->onSignalBarsUpdated_(signalBars); } } + +private: + tgvoip::VoIPController *controller_; + std::function onStateUpdated_; + std::function onSignalBarsUpdated_; + std::mutex m_onStateUpdated, m_onSignalBarsUpdated; }; std::function globalLoggingFunction; @@ -384,7 +395,7 @@ void __tgvoip_call_tglog(const char *format, ...) va_list vaCopy; va_copy(vaCopy, vaArgs); - const int length = std::vsnprintf(NULL, 0, format, vaCopy); + const int length = std::vsnprintf(nullptr, 0, format, vaCopy); va_end(vaCopy); std::vector zc(length + 1); @@ -407,6 +418,16 @@ void TgVoip::setGlobalServerConfig(const std::string &serverConfig) tgvoip::ServerConfig::GetSharedInstance()->Update(serverConfig); } +int TgVoip::getConnectionMaxLayer() +{ + return tgvoip::VoIPController::GetConnectionMaxLayer(); +} + +std::string TgVoip::getVersion() +{ + return tgvoip::VoIPController::GetVersion(); +} + TgVoip *TgVoip::makeInstance( TgVoipConfig const &config, TgVoipPersistentState const &persistentState, @@ -442,6 +463,4 @@ TgVoip *TgVoip::makeInstance( ); } -TgVoip::~TgVoip() -{ -} +TgVoip::~TgVoip() = default; diff --git a/TgVoip.h b/TgVoip.h index 28dba52..91a964b 100755 --- a/TgVoip.h +++ b/TgVoip.h @@ -22,7 +22,7 @@ enum class TgVoipEndpointType TcpRelay }; -struct TgVoipEndpointHost +struct TgVoipEdpointHost { std::string ipv4; std::string ipv6; @@ -31,7 +31,7 @@ struct TgVoipEndpointHost struct TgVoipEndpoint { int64_t endpointId; - TgVoipEndpointHost host; + TgVoipEdpointHost host; uint16_t port; TgVoipEndpointType type; unsigned char peerTag[16]; @@ -89,7 +89,11 @@ struct TgVoipConfig bool enableNS; bool enableAGC; bool enableCallUpgrade; +#ifndef _WIN32 std::string logPath; +#else + std::wstring logPath; +#endif int maxApiLayer; }; diff --git a/aclocal.m4 b/aclocal.m4 index 5015cc9..3731753 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -8963,11 +8963,11 @@ m4_define([lt_dict_filter], # serial 4221 ltversion.m4 # This file is part of GNU Libtool -m4_define([LT_PACKAGE_VERSION], [2.4.6.42-b88ce]) +m4_define([LT_PACKAGE_VERSION], [2.4.6.42-b88ce-dirty]) m4_define([LT_PACKAGE_REVISION], [2.4.6.42]) AC_DEFUN([LTVERSION_VERSION], -[macro_version='2.4.6.42-b88ce' +[macro_version='2.4.6.42-b88ce-dirty' macro_revision='2.4.6.42' _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) _LT_DECL(, macro_revision, 0) diff --git a/configure b/configure index 5398586..421c1c9 100755 --- a/configure +++ b/configure @@ -3270,7 +3270,7 @@ esac -macro_version='2.4.6.42-b88ce' +macro_version='2.4.6.42-b88ce-dirty' macro_revision='2.4.6.42' diff --git a/controller/audio/AudioPacketSender.cpp b/controller/audio/AudioPacketSender.cpp index 43f6d9f..b6a0c94 100644 --- a/controller/audio/AudioPacketSender.cpp +++ b/controller/audio/AudioPacketSender.cpp @@ -79,7 +79,7 @@ void AudioPacketSender::SendFrame(unsigned char *data, size_t len, unsigned char if (hasExtraFEC) { Buffer ecBuf(secondaryLen); - ecBuf.CopyFrom(*secondaryDataBufPtr, 0, secondaryLen); + ecBuf.CopyFromOtherBuffer(*secondaryDataBufPtr, secondaryLen); if (ecAudioPackets.size() == 4) { ecAudioPackets.pop_front(); @@ -135,7 +135,7 @@ void AudioPacketSender::SendFrame(unsigned char *data, size_t len, unsigned char if (PeerVersion() < 7 && secondaryLen && shittyInternetMode) { Buffer ecBuf(secondaryLen); - ecBuf.CopyFrom(*secondaryDataBufPtr, 0, secondaryLen); + ecBuf.CopyFromOtherBuffer(*secondaryDataBufPtr, secondaryLen); if (ecAudioPackets.size() == 4) { ecAudioPackets.pop_front(); diff --git a/controller/audio/EchoCanceller.cpp b/controller/audio/EchoCanceller.cpp index ef1fa77..686e8ce 100755 --- a/controller/audio/EchoCanceller.cpp +++ b/controller/audio/EchoCanceller.cpp @@ -99,11 +99,12 @@ EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC) EchoCanceller::~EchoCanceller() { #ifndef TGVOIP_NO_DSP - delete apm; - delete audioFrame; farendQueue->Put(Buffer()); bufferFarendThread->Join(); delete bufferFarendThread; + delete farendQueue; + delete audioFrame; + delete apm; #endif } diff --git a/controller/media/MediaStreamItf.cpp b/controller/media/MediaStreamItf.cpp index 295cabf..1f7303d 100644 --- a/controller/media/MediaStreamItf.cpp +++ b/controller/media/MediaStreamItf.cpp @@ -16,12 +16,15 @@ using namespace tgvoip; void MediaStreamItf::SetCallback(size_t (*f)(unsigned char *, size_t, void *), void *param) { + MutexGuard m(callbackMutex); + callback = f; callbackParam = param; } size_t MediaStreamItf::InvokeCallback(unsigned char *data, size_t length) { + MutexGuard m(callbackMutex); if (callback) return (*callback)(data, length, callbackParam); return 0; diff --git a/controller/media/MediaStreamItf.h b/controller/media/MediaStreamItf.h index 134917f..d16f7d7 100644 --- a/controller/media/MediaStreamItf.h +++ b/controller/media/MediaStreamItf.h @@ -33,6 +33,7 @@ public: private: size_t (*callback)(unsigned char *, size_t, void *) = NULL; void *callbackParam; + Mutex callbackMutex; }; class AudioMixer : public MediaStreamItf diff --git a/controller/net/JitterBuffer.cpp b/controller/net/JitterBuffer.cpp index 18be2b9..b47c11a 100755 --- a/controller/net/JitterBuffer.cpp +++ b/controller/net/JitterBuffer.cpp @@ -99,7 +99,7 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) //LOGV("Found existing packet for timestamp %u, overwrite %d", pkt->timestamp, overwriteExisting); if (overwriteExisting) { - slots[i].buffer.CopyFrom(pkt->buffer, pkt->size); + slots[i].buffer.CopyFromOtherBuffer(pkt->buffer, pkt->size); slots[i].size = pkt->size; slots[i].isEC = pkt->isEC; } @@ -113,7 +113,7 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) outstandingDelayChange = 0; nextFetchTimestamp = static_cast(static_cast(pkt->timestamp) - step * minDelay); first = true; - LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextFetchTimestamp, step, minDelay); + LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextFetchTimestamp, step, (double)minDelay); } for (i = 0; i < JITTER_SLOT_COUNT; i++) @@ -194,7 +194,7 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) slots[i].buffer = bufferPool.Get(); slots[i].recvTimeDiff = time - prevRecvTime; slots[i].isEC = pkt->isEC; - slots[i].buffer.CopyFrom(pkt->buffer, pkt->size); + slots[i].buffer.CopyFromOtherBuffer(pkt->buffer, pkt->size); #ifdef TGVOIP_DUMP_JITTER_STATS fprintf(dump, "%u\t%.03f\t%d\t%.03f\t%.03f\t%.03f\n", pkt->timestamp, time, GetCurrentDelay(), lastMeasuredJitter, lastMeasuredDelay, minDelay); #endif @@ -322,7 +322,7 @@ int JitterBuffer::GetInternal(jitter_packet_t *pkt, int offset, bool advance) { pkt->size = slots[i].size; pkt->timestamp = slots[i].timestamp; - pkt->buffer.CopyFrom(slots[i].buffer, slots[i].size); + pkt->buffer.CopyFromOtherBuffer(slots[i].buffer, slots[i].size); pkt->isEC = slots[i].isEC; } } @@ -428,7 +428,7 @@ void JitterBuffer::Tick() if ((diff > 0 && dontIncMinDelay == 0) || (diff < 0 && dontDecMinDelay == 0)) { //nextFetchTimestamp+=diff*(int32_t)step; - minDelay += diff; + minDelay.store(minDelay + diff); outstandingDelayChange += diff * 60; dontChangeDelay += 32; //LOGD("new delay from stddev %f", minDelay); diff --git a/controller/net/JitterBuffer.h b/controller/net/JitterBuffer.h index 095b8d8..7b9b658 100644 --- a/controller/net/JitterBuffer.h +++ b/controller/net/JitterBuffer.h @@ -73,8 +73,8 @@ private: Mutex mutex; uint32_t step; std::array slots{0}; - std::atomic nextFetchTimestamp = ATOMIC_VAR_INIT(0); // What frame to read next - double minDelay = 6; + std::atomic nextFetchTimestamp{0}; // What frame to read next (protected for GetSeqTooLate) + std::atomic minDelay{6}; uint32_t minMinDelay; uint32_t maxMinDelay; uint32_t maxUsedSlots; diff --git a/controller/protocol/Reliable.cpp b/controller/protocol/Reliable.cpp index 10e5422..add1f55 100644 --- a/controller/protocol/Reliable.cpp +++ b/controller/protocol/Reliable.cpp @@ -64,7 +64,7 @@ void VoIPController::UpdateReliablePackets() if (qp->firstSentTime == 0) qp->firstSentTime = qp->lastSentTime; if (qp->data.Length()) - buf.CopyFrom(qp->data, qp->data.Length()); + buf.CopyFromOtherBuffer(qp->data, qp->data.Length()); packetsToSend.push_back(PendingOutgoingPacket{ /*.seq=*/seq, /*.type=*/qp->type, diff --git a/tools/Buffers.cpp b/tools/Buffers.cpp index 2120ea4..66c683f 100644 --- a/tools/Buffers.cpp +++ b/tools/Buffers.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include "tools/logging.h" using namespace tgvoip; @@ -145,7 +145,7 @@ BufferOutputStream::BufferOutputStream(size_t size_) bufferProvided(false) { - buffer = (unsigned char *)malloc(size_); + buffer = reinterpret_cast(std::malloc(size_)); if (!buffer) throw std::bad_alloc(); bufferProvided = false; @@ -239,18 +239,16 @@ void BufferOutputStream::ExpandBufferIfNeeded(size_t need) { throw std::out_of_range("buffer overflow"); } - if (need < 1024) + size += std::max(need, size_t{1024}); + unsigned char *newBuffer = reinterpret_cast(std::realloc(buffer, size)); + if (!newBuffer) { - buffer = (unsigned char *)realloc(buffer, size + 1024); - size += 1024; - } - else - { - buffer = (unsigned char *)realloc(buffer, size + need); - size += need; - } - if (!buffer) + std::free(buffer); + buffer = nullptr; + size = 0; throw std::bad_alloc(); + } + buffer = newBuffer; } } diff --git a/tools/Buffers.h b/tools/Buffers.h index aaf61c4..37cfc89 100644 --- a/tools/Buffers.h +++ b/tools/Buffers.h @@ -211,7 +211,7 @@ public: { return data; } - void CopyFrom(const Buffer &other, size_t count, size_t srcOffset = 0, size_t dstOffset = 0) + void CopyFromOtherBuffer(const Buffer &other, size_t count, size_t srcOffset = 0, size_t dstOffset = 0) { if (!other.data) throw std::invalid_argument("CopyFrom can't copy from NULL"); @@ -248,7 +248,7 @@ public: if (other.IsEmpty()) return Buffer(); Buffer buf(other.length); - buf.CopyFrom(other, other.length); + buf.CopyFromOtherBuffer(other, other.length); return buf; } static Buffer CopyOf(const Buffer &other, size_t offset, size_t length) @@ -256,7 +256,7 @@ public: if (offset + length > other.Length()) throw std::out_of_range("offset+length out of bounds"); Buffer buf(length); - buf.CopyFrom(other, length, offset); + buf.CopyFromOtherBuffer(other, length, offset); return buf; } static Buffer Wrap(unsigned char *data, size_t size, const std::function &freeFn, const std::function &reallocFn) diff --git a/tools/MessageThread.cpp b/tools/MessageThread.cpp index 812dc65..db5ff6f 100755 --- a/tools/MessageThread.cpp +++ b/tools/MessageThread.cpp @@ -18,9 +18,8 @@ using namespace tgvoip; -MessageThread::MessageThread() : Thread(std::bind(&MessageThread::Run, this)) +MessageThread::MessageThread() : Thread(std::bind(&MessageThread::Run, this)), running(true) { - running = true; SetName("MessageThread"); #ifdef _WIN32 diff --git a/video/VideoFEC.cpp b/video/VideoFEC.cpp index 7f24219..1139cbb 100644 --- a/video/VideoFEC.cpp +++ b/video/VideoFEC.cpp @@ -27,7 +27,7 @@ Buffer ParityFEC::Encode(std::vector& packets){ _result[maxSize+1] ^= (uint8_t)(len >> 8); } - return std::move(result); + return result; } Buffer ParityFEC::Decode(std::vector& dataPackets, Buffer &fecPacket){ @@ -68,5 +68,5 @@ Buffer ParityFEC::Decode(std::vector& dataPackets, Buffer &fecPacket){ LOGV("ParityFEC decoded packet size %u", len); result.Resize(len); - return std::move(result); + return result; } diff --git a/webrtc_dsp/common_audio/signal_processing/filter_ar.c b/webrtc_dsp/common_audio/signal_processing/filter_ar.c index 2471cd1..1b7a694 100644 --- a/webrtc_dsp/common_audio/signal_processing/filter_ar.c +++ b/webrtc_dsp/common_audio/signal_processing/filter_ar.c @@ -47,7 +47,7 @@ size_t WebRtcSpl_FilterAR(const int16_t* a, // because of this. int filtered_ix = (int)i - 1; int16_t* state_ptr = &state[state_length - 1]; - int16_t* state_low_ptr = &state_low[state_length - 1]; + int16_t* state_low_ptr = &state_low[state_low_length - 1]; o = (int32_t)(*x_ptr++) * (1 << 12); oLOW = (int32_t)0; @@ -87,7 +87,7 @@ size_t WebRtcSpl_FilterAR(const int16_t* a, for (i = 0; i < x_length; i++) { state[state_length - x_length + i] = filtered[i]; - state[state_length - x_length + i] = filtered_low[i]; + state_low[state_low_length - x_length + i] = filtered_low[i]; } } diff --git a/webrtc_dsp/rtc_base/numerics/safe_minmax.h b/webrtc_dsp/rtc_base/numerics/safe_minmax.h index 8d00afb..280b0d5 100644 --- a/webrtc_dsp/rtc_base/numerics/safe_minmax.h +++ b/webrtc_dsp/rtc_base/numerics/safe_minmax.h @@ -182,7 +182,7 @@ struct TypeOr { "The specified type isn't large enough"); static_assert(IsIntlike::value == IsIntlike::value && std::is_floating_point::value == - std::is_floating_point::value, + std::is_floating_point::value, "float<->int conversions not allowed"); };