From 9e686b6122d69cf3f4ededbe01b3383ced73be78 Mon Sep 17 00:00:00 2001 From: Crend King <975235+CrendKing@users.noreply.github.com> Date: Fri, 2 Aug 2024 15:42:58 -0700 Subject: [PATCH] Lock the source mutex before acquiring the condition variable --- avisynth_filter/src/frame_handler.cpp | 32 +++++++++++++----------- vapoursynth_filter/src/frame_handler.cpp | 32 +++++++++++++----------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/avisynth_filter/src/frame_handler.cpp b/avisynth_filter/src/frame_handler.cpp index e84f2c5..e3384ca 100644 --- a/avisynth_filter/src/frame_handler.cpp +++ b/avisynth_filter/src/frame_handler.cpp @@ -11,24 +11,28 @@ namespace SynthFilter { auto FrameHandler::AddInputSample(IMediaSample *inputSample) -> HRESULT { HRESULT hr; - _addInputSampleCv.wait(_filter.m_csReceive, [this]() -> bool { - if (_isFlushing) { - return true; - } + { + std::shared_lock sharedSourceLock(_sourceMutex); - if (_nextSourceFrameNb <= Environment::GetInstance().GetInitialSrcBuffer()) { - return true; - } + _addInputSampleCv.wait(sharedSourceLock, [this]() -> bool { + if (_isFlushing) { + return true; + } - UpdateExtraSrcBuffer(); + if (_nextSourceFrameNb <= Environment::GetInstance().GetInitialSrcBuffer()) { + return true; + } - // at least NUM_SRC_FRAMES_PER_PROCESSING source frames are needed in queue for stop time calculation - if (static_cast(_sourceFrames.size()) < NUM_SRC_FRAMES_PER_PROCESSING + _extraSrcBuffer) { - return true; - } + UpdateExtraSrcBuffer(); - return _nextSourceFrameNb <= _maxRequestedFrameNb; - }); + // at least NUM_SRC_FRAMES_PER_PROCESSING source frames are needed in queue for stop time calculation + if (static_cast(_sourceFrames.size()) < NUM_SRC_FRAMES_PER_PROCESSING + _extraSrcBuffer) { + return true; + } + + return _nextSourceFrameNb <= _maxRequestedFrameNb; + }); + } if (_isFlushing || _isStopping) { Environment::GetInstance().Log(L"Reject input sample due to flush or stop"); diff --git a/vapoursynth_filter/src/frame_handler.cpp b/vapoursynth_filter/src/frame_handler.cpp index ec7089d..21d818c 100644 --- a/vapoursynth_filter/src/frame_handler.cpp +++ b/vapoursynth_filter/src/frame_handler.cpp @@ -11,24 +11,28 @@ namespace SynthFilter { auto FrameHandler::AddInputSample(IMediaSample *inputSample) -> HRESULT { HRESULT hr; - _addInputSampleCv.wait(_filter.m_csReceive, [this]() -> bool { - if (_isFlushing) { - return true; - } + { + std::shared_lock sharedSourceLock(_sourceMutex); - if (_nextSourceFrameNb <= Environment::GetInstance().GetInitialSrcBuffer()) { - return true; - } + _addInputSampleCv.wait(sharedSourceLock, [this]() -> bool { + if (_isFlushing) { + return true; + } - UpdateExtraSrcBuffer(); + if (_nextSourceFrameNb <= Environment::GetInstance().GetInitialSrcBuffer()) { + return true; + } - // at least NUM_SRC_FRAMES_PER_PROCESSING source frames are needed in queue for stop time calculation - if (static_cast(_sourceFrames.size()) < NUM_SRC_FRAMES_PER_PROCESSING + _extraSrcBuffer) { - return true; - } + UpdateExtraSrcBuffer(); - return _nextSourceFrameNb <= _lastUsedSourceFrameNb + Environment::GetInstance().GetInitialSrcBuffer() + NUM_SRC_FRAMES_PER_PROCESSING; - }); + // at least NUM_SRC_FRAMES_PER_PROCESSING source frames are needed in queue for stop time calculation + if (static_cast(_sourceFrames.size()) < NUM_SRC_FRAMES_PER_PROCESSING + _extraSrcBuffer) { + return true; + } + + return _nextSourceFrameNb <= _lastUsedSourceFrameNb + Environment::GetInstance().GetInitialSrcBuffer() + NUM_SRC_FRAMES_PER_PROCESSING; + }); + } if (_isFlushing || _isStopping) { Environment::GetInstance().Log(L"Reject input sample due to flush or stop");