From 99530863d5fa852ec68797be3930146fc2d049ed Mon Sep 17 00:00:00 2001 From: narspt Date: Fri, 19 Aug 2022 21:22:10 +0100 Subject: [PATCH] fix openstream timeout issue --- src/csemaphore.cpp | 17 ++++++++++++++++- src/csemaphore.h | 2 ++ src/ctranscoder.cpp | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/csemaphore.cpp b/src/csemaphore.cpp index 3b91e82..8f22d92 100644 --- a/src/csemaphore.cpp +++ b/src/csemaphore.cpp @@ -32,6 +32,7 @@ CSemaphore::CSemaphore() { // Initialized as locked. m_Count = 0; + m_WaitingCount = 0; } //////////////////////////////////////////////////////////////////////////////////////// @@ -66,7 +67,21 @@ bool CSemaphore::WaitFor(uint ms) { m_Count--; } + if ( m_WaitingCount > 0 ) + { + m_WaitingCount--; + } return ok; - } +void CSemaphore::PreWaitFor(void) +{ + std::unique_lock lock(m_Mutex); + // discard timedout notify(s) count + if ( m_Count > m_WaitingCount ) + { + m_Count = m_WaitingCount; + } + // pre flag waiting notify + m_WaitingCount++; +} diff --git a/src/csemaphore.h b/src/csemaphore.h index 0729512..0117fba 100644 --- a/src/csemaphore.h +++ b/src/csemaphore.h @@ -43,12 +43,14 @@ public: void Notify(void); void Wait(void); bool WaitFor(uint); + void PreWaitFor(void); protected: // data std::mutex m_Mutex; std::condition_variable m_Condition; size_t m_Count; + size_t m_WaitingCount; }; diff --git a/src/ctranscoder.cpp b/src/ctranscoder.cpp index 668b058..a803f24 100644 --- a/src/ctranscoder.cpp +++ b/src/ctranscoder.cpp @@ -216,6 +216,7 @@ CCodecStream *CTranscoder::GetStream(CPacketStream *PacketStream, uint8 uiCodecI { // yes, post openstream request EncodeOpenstreamPacket(&Buffer, uiCodecIn, (uiCodecIn == CODEC_AMBEPLUS) ? CODEC_AMBE2PLUS : CODEC_AMBEPLUS); + m_SemaphoreOpenStream.PreWaitFor(); // pre flag waiting notify and discard timedout notify(s) count m_Socket.Send(Buffer, m_Ip, TRANSCODER_PORT); // wait relpy here