From 14b034e94c857239327ab2d9516a797a2cdaa7e1 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Thu, 21 Mar 2019 18:40:05 +0200 Subject: [PATCH] - cleanups --- src/engine/helper/HL_Singletone.h | 48 +++++++++++++++++++-------- src/engine/helper/HL_Sync.h | 11 +++--- src/engine/media/MT_AudioReceiver.cpp | 38 +++++++++++---------- src/engine/media/MT_AudioReceiver.h | 14 ++++---- 4 files changed, 69 insertions(+), 42 deletions(-) diff --git a/src/engine/helper/HL_Singletone.h b/src/engine/helper/HL_Singletone.h index 4999236f..9473c153 100644 --- a/src/engine/helper/HL_Singletone.h +++ b/src/engine/helper/HL_Singletone.h @@ -8,26 +8,46 @@ template class SafeSingleton { protected: - static std::atomic SharedInstance; - static std::mutex mMutex; + static std::atomic SharedInstance; + static std::mutex mMutex; public: - static T& instance() - { - T* tmp = SharedInstance.load(std::memory_order_relaxed); - std::atomic_thread_fence(std::memory_order_acquire); - if (tmp == nullptr) + static T& instance() { - std::lock_guard lock(mMutex); - tmp = SharedInstance.load(std::memory_order_relaxed); + T* tmp = SharedInstance.load(std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_acquire); if (tmp == nullptr) { - tmp = new T(); - std::atomic_thread_fence(std::memory_order_release); - SharedInstance.store(tmp, std::memory_order_relaxed); + std::lock_guard lock(mMutex); + tmp = SharedInstance.load(std::memory_order_relaxed); + if (tmp == nullptr) + { + tmp = new T(); + std::atomic_thread_fence(std::memory_order_release); + SharedInstance.store(tmp, std::memory_order_relaxed); + } } + return *tmp; + } + + template + static T& precreate(X n) + { + T* tmp = SharedInstance.load(std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_acquire); + if (tmp == nullptr) + { + std::lock_guard lock(mMutex); + tmp = SharedInstance.load(std::memory_order_relaxed); + if (tmp == nullptr) + { + tmp = new T(n); + std::atomic_thread_fence(std::memory_order_release); + SharedInstance.store(tmp, std::memory_order_relaxed); + return *tmp; + } + } + throw std::runtime_error("Singletone instance is created already"); } - return *tmp; - } }; template diff --git a/src/engine/helper/HL_Sync.h b/src/engine/helper/HL_Sync.h index 6bd57531..5c512456 100644 --- a/src/engine/helper/HL_Sync.h +++ b/src/engine/helper/HL_Sync.h @@ -1,4 +1,4 @@ -/* Copyright(C) 2007-2018 VoIPobjects (voipobjects.com) +/* Copyright(C) 2007-2019 VoIPobjects (voipobjects.com) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -99,7 +99,8 @@ protected: // - Handlers are ALWAYS executed in the Timer Queue worker thread. // - Handlers execution order is NOT guaranteed // -class TimerQueue { +class TimerQueue +{ public: TimerQueue(); ~TimerQueue(); @@ -135,7 +136,8 @@ private: bool m_finish = false; uint64_t m_idcounter = 0; - struct WorkItem { + struct WorkItem + { Clock::time_point end; uint64_t id; // id==0 means it was cancelled std::function handler; @@ -145,7 +147,8 @@ private: std::mutex m_mtx; // Inheriting from priority_queue, so we can access the internal container class Queue : public std::priority_queue, - std::greater> { + std::greater> + { public: std::vector& getContainer(); } m_items; diff --git a/src/engine/media/MT_AudioReceiver.cpp b/src/engine/media/MT_AudioReceiver.cpp index 8473e23a..a0247370 100644 --- a/src/engine/media/MT_AudioReceiver.cpp +++ b/src/engine/media/MT_AudioReceiver.cpp @@ -92,7 +92,7 @@ int RtpBuffer::prebuffer() int RtpBuffer::getCount() const { Lock l(mGuard); - return mPacketList.size(); + return static_cast(mPacketList.size()); } bool SequenceSort(const RtpBuffer::Packet& p1, const RtpBuffer::Packet& p2) @@ -390,7 +390,7 @@ bool AudioReceiver::add(std::shared_ptr p, Codec** codec) // Process jitter mJitterStats.process(p.get(), codecIter->second->samplerate()); - mStat.mJitter = (float)mJitterStats.get().getCurrent(); + mStat.mJitter = static_cast(mJitterStats.get().getCurrent()); // Check if packet is CNG if (payloadLength >= 1 && payloadLength <= 6 && (ptype == 0 || ptype == 8)) @@ -409,14 +409,14 @@ bool AudioReceiver::add(std::shared_ptr p, Codec** codec) return mBuffer.add(p, timelen, codecIter->second->samplerate()); } -void AudioReceiver::processDecoded(Audio::DataWindow& output, DecodeOptions options) +void AudioReceiver::processDecoded(Audio::DataWindow& output, int options) { // Write to audio dump if requested if (mDecodedDump && mDecodedLength) mDecodedDump->write(mDecodedFrame, mDecodedLength); // Resample to target rate - bool resample = !((int)options & (int)DecodeOptions::DontResample); + bool resample = !(options & DecodeOptions_DontResample); makeMonoAndResample(resample ? mCodec->samplerate() : 0, mCodec->channels()); @@ -429,7 +429,7 @@ void AudioReceiver::processDecoded(Audio::DataWindow& output, DecodeOptions opti output.add(mResampledFrame, mResampledLength); } -bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, int* rate) +bool AudioReceiver::getAudio(Audio::DataWindow& output, int options, int* rate) { bool result = false; @@ -446,13 +446,14 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i { // Synthesize comfort noise. It will be done on AUDIO_SAMPLERATE rate directly to mResampledFrame buffer. // Do not forget to send this noise to analysis - mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), mLastPacketTimeLength, (short*)mDecodedFrame, false); + mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), mLastPacketTimeLength, + reinterpret_cast(mDecodedFrame), false); } else if (mCodec && mFrameCount && !mCodecSettings.mSkipDecode) { // Do PLC to mDecodedFrame/mDecodedLength - if ((int)options & (int)DecodeOptions::SkipDecode) + if (options & DecodeOptions_SkipDecode) mDecodedLength = 0; else mDecodedLength = mCodec->plc(mFrameCount, mDecodedFrame, sizeof mDecodedFrame); @@ -481,7 +482,7 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i { assert(p); // Check if previously CNG packet was detected. Emit CNG audio here if needed. - if ((int)options & (int)DecodeOptions::FillCngGap && mCngPacket && mCodec) + if (options & DecodeOptions_FillCngGap && mCngPacket && mCodec) { // Fill CNG audio is server mode is present int units = p->GetTimestamp() - mCngPacket->GetTimestamp(); @@ -491,10 +492,11 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i int frames100ms = milliseconds / 100; for (int frameIndex = 0; frameIndex < frames100ms; frameIndex++) { - if ((int)options & (int)DecodeOptions::SkipDecode) + if (options & DecodeOptions_SkipDecode) mDecodedLength = 0; else - mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), 100, (short*)mDecodedFrame, false); + mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), 100, + reinterpret_cast(mDecodedFrame), false); if (mDecodedLength) processDecoded(output, options); @@ -503,10 +505,11 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i int tail = milliseconds % 100; if (tail) { - if ((int)options & (int)DecodeOptions::SkipDecode) + if (options & DecodeOptions_SkipDecode) mDecodedLength = 0; else - mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), tail, (short*)mDecodedFrame, false); + mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), tail, + reinterpret_cast(mDecodedFrame), false); if (mDecodedLength) processDecoded(output, options); @@ -525,14 +528,15 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i // Check if it is CNG packet if ((p->GetPayloadType() == 0 || p->GetPayloadType() == 8) && p->GetPayloadLength() >= 1 && p->GetPayloadLength() <= 6) { - if ((int)options & (int)DecodeOptions::SkipDecode) + if (options & DecodeOptions_SkipDecode) mDecodedLength = 0; else { mCngPacket = p; mCngDecoder.decode3389(p->GetPayloadData(), p->GetPayloadLength()); // Emit CNG mLastPacketLength milliseconds - mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), mLastPacketTimeLength, (short*)mDecodedFrame, true); + mDecodedLength = mCngDecoder.produce(mCodec->samplerate(), mLastPacketTimeLength, + (short*)mDecodedFrame, true); if (mDecodedLength) processDecoded(output, options); } @@ -559,7 +563,7 @@ bool AudioReceiver::getAudio(Audio::DataWindow& output, DecodeOptions options, i // Decode for (int i=0; isecond; if (codec) { - int frameCount = p.GetPayloadLength() / codec->rtpLength(); + int frameCount = static_cast(p.GetPayloadLength() / codec->rtpLength()); if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength()) frameCount++; @@ -758,6 +762,6 @@ DtmfReceiver::~DtmfReceiver() { } -void DtmfReceiver::add(std::shared_ptr p) +void DtmfReceiver::add(std::shared_ptr /*p*/) { } diff --git a/src/engine/media/MT_AudioReceiver.h b/src/engine/media/MT_AudioReceiver.h index 281aa60b..aeb7a178 100644 --- a/src/engine/media/MT_AudioReceiver.h +++ b/src/engine/media/MT_AudioReceiver.h @@ -113,15 +113,15 @@ namespace MT bool add(std::shared_ptr p, Codec** codec = nullptr); // Returns false when there is no rtp data from jitter - enum class DecodeOptions + enum DecodeOptions { - ResampleToMainRate = 0, - DontResample = 1, - FillCngGap = 2, - SkipDecode = 4 + DecodeOptions_ResampleToMainRate = 0, + DecodeOptions_DontResample = 1, + DecodeOptions_FillCngGap = 2, + DecodeOptions_SkipDecode = 4 }; - bool getAudio(Audio::DataWindow& output, DecodeOptions options = DecodeOptions::ResampleToMainRate, int* rate = nullptr); + bool getAudio(Audio::DataWindow& output, int options = DecodeOptions_ResampleToMainRate, int* rate = nullptr); // Looks for codec by payload type Codec* findCodec(int payloadType); @@ -172,7 +172,7 @@ namespace MT void makeMonoAndResample(int rate, int channels); // Resamples, sends to analysis, writes to dump and queues to output decoded frames from mDecodedFrame - void processDecoded(Audio::DataWindow& output, DecodeOptions options); + void processDecoded(Audio::DataWindow& output, int options); #if defined(USE_PVQA_LIBRARY) && !defined(PVQA_SERVER) std::shared_ptr mPVQA;