diff --git a/avisynth_filter/src/frame_handler.cpp b/avisynth_filter/src/frame_handler.cpp index 554fb97..d8e5eff 100644 --- a/avisynth_filter/src/frame_handler.cpp +++ b/avisynth_filter/src/frame_handler.cpp @@ -149,15 +149,7 @@ auto FrameHandler::BeginFlush() -> void { auto FrameHandler::EndFlush(const std::function &interim) -> void { Environment::GetInstance().Log(L"FrameHandler start EndFlush()"); - /* - * EndFlush() can be called by either the application or the worker thread - * - * when called by former, we need to synchronize with the worker thread - * when called by latter, current thread ID should be same as the worker thread, and no need for sync - */ - if (std::this_thread::get_id() != _workerThread.get_id()) { - _isWorkerLatched.wait(false); - } + _isWorkerLatched.wait(false); if (interim) { interim(); @@ -207,16 +199,6 @@ auto FrameHandler::ResetInput() -> void { _currentInputFrameRate = 0; } -auto FrameHandler::ResetOutput() -> void { - // to ensure these non-atomic properties are only modified by their sole consumer the worker thread - ASSERT(std::this_thread::get_id() == _workerThread.get_id()); - - _nextOutputFrameNb = 0; - - _frameRateCheckpointOutputFrameNb = 0; - _currentOutputFrameRate = 0; -} - auto FrameHandler::PrepareOutputSample(ATL::CComPtr &sample, REFERENCE_TIME startTime, REFERENCE_TIME stopTime) -> bool { if (FAILED(_filter.m_pOutput->GetDeliveryBuffer(&sample, &startTime, &stopTime, 0))) { // avoid releasing the invalid pointer in case the function change it to some random invalid address @@ -266,6 +248,13 @@ auto FrameHandler::PrepareOutputSample(ATL::CComPtr &sample, REFER } auto FrameHandler::WorkerProc() -> void { + const auto ResetOutput = [this]() -> void { + _nextOutputFrameNb = 0; + + _frameRateCheckpointOutputFrameNb = 0; + _currentOutputFrameRate = 0; + }; + Environment::GetInstance().Log(L"Start worker thread"); #ifdef _DEBUG diff --git a/avisynth_filter/src/frame_handler.h b/avisynth_filter/src/frame_handler.h index acb9d1d..31f0a13 100644 --- a/avisynth_filter/src/frame_handler.h +++ b/avisynth_filter/src/frame_handler.h @@ -40,7 +40,6 @@ class FrameHandler { int ¤tFrameRate) -> void; auto ResetInput() -> void; - auto ResetOutput() -> void; auto PrepareOutputSample(ATL::CComPtr &sample, REFERENCE_TIME startTime, REFERENCE_TIME stopTime) -> bool; auto WorkerProc() -> void; auto GarbageCollect(int srcFrameNb) -> void; diff --git a/vapoursynth_filter/src/frame_handler.cpp b/vapoursynth_filter/src/frame_handler.cpp index 6992155..a7c4cc8 100644 --- a/vapoursynth_filter/src/frame_handler.cpp +++ b/vapoursynth_filter/src/frame_handler.cpp @@ -203,15 +203,7 @@ auto FrameHandler::BeginFlush() -> void { auto FrameHandler::EndFlush(const std::function &interim) -> void { Environment::GetInstance().Log(L"FrameHandler start EndFlush()"); - /* - * EndFlush() can be called by either the application or the worker thread - * - * when called by former, we need to synchronize with the worker thread - * when called by latter, current thread ID should be same as the worker thread, and no need for sync - */ - if (std::this_thread::get_id() != _workerThread.get_id()) { - _isWorkerLatched.wait(false); - } + _isWorkerLatched.wait(false); { std::shared_lock sharedOutputLock(_outputMutex); @@ -311,18 +303,6 @@ auto FrameHandler::ResetInput() -> void { _frameRateCheckpointInputSampleNb = 0; _currentInputFrameRate = 0; - - _frameRateCheckpointOutputFrameNb = 0; - _currentOutputFrameRate = 0; -} - -auto FrameHandler::ResetOutput() -> void { - // to ensure these non-atomic properties are only modified by their sole consumer the worker thread - const std::thread::id actual = std::this_thread::get_id(); - const std::thread::id expected = _workerThread.get_id(); - ASSERT(actual == expected); - - _nextDeliveryFrameNb = 0; } auto FrameHandler::PrepareOutputSample(ATL::CComPtr &sample, int frameNb, OutputFrameData &data) -> bool { @@ -393,6 +373,13 @@ auto FrameHandler::PrepareOutputSample(ATL::CComPtr &sample, int f } auto FrameHandler::WorkerProc() -> void { + const auto ResetOutput = [this]() -> void { + _nextDeliveryFrameNb = 0; + + _frameRateCheckpointOutputFrameNb = 0; + _currentOutputFrameRate = 0; + }; + Environment::GetInstance().Log(L"Start worker thread"); #ifdef _DEBUG diff --git a/vapoursynth_filter/src/frame_handler.h b/vapoursynth_filter/src/frame_handler.h index f6ec812..71343e4 100644 --- a/vapoursynth_filter/src/frame_handler.h +++ b/vapoursynth_filter/src/frame_handler.h @@ -51,7 +51,6 @@ class FrameHandler { int ¤tFrameRate) -> void; auto ResetInput() -> void; - auto ResetOutput() -> void; auto PrepareOutputSample(ATL::CComPtr &sample, int frameNb, OutputFrameData &data) -> bool; auto WorkerProc() -> void; auto GarbageCollect(int srcFrameNb) -> void;