Skip to content

Commit

Permalink
Move ResetOutput() into the worker thread proc
Browse files Browse the repository at this point in the history
Simplify EndFlush()
  • Loading branch information
CrendKing committed Jun 16, 2021
1 parent 55727fb commit 391466f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
27 changes: 8 additions & 19 deletions avisynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,7 @@ auto FrameHandler::BeginFlush() -> void {
auto FrameHandler::EndFlush(const std::function<void ()> &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();
Expand Down Expand Up @@ -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<IMediaSample> &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
Expand Down Expand Up @@ -266,6 +248,13 @@ auto FrameHandler::PrepareOutputSample(ATL::CComPtr<IMediaSample> &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
Expand Down
1 change: 0 additions & 1 deletion avisynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class FrameHandler {
int &currentFrameRate) -> void;

auto ResetInput() -> void;
auto ResetOutput() -> void;
auto PrepareOutputSample(ATL::CComPtr<IMediaSample> &sample, REFERENCE_TIME startTime, REFERENCE_TIME stopTime) -> bool;
auto WorkerProc() -> void;
auto GarbageCollect(int srcFrameNb) -> void;
Expand Down
29 changes: 8 additions & 21 deletions vapoursynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,7 @@ auto FrameHandler::BeginFlush() -> void {
auto FrameHandler::EndFlush(const std::function<void ()> &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);
Expand Down Expand Up @@ -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<IMediaSample> &sample, int frameNb, OutputFrameData &data) -> bool {
Expand Down Expand Up @@ -393,6 +373,13 @@ auto FrameHandler::PrepareOutputSample(ATL::CComPtr<IMediaSample> &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
Expand Down
1 change: 0 additions & 1 deletion vapoursynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class FrameHandler {
int &currentFrameRate) -> void;

auto ResetInput() -> void;
auto ResetOutput() -> void;
auto PrepareOutputSample(ATL::CComPtr<IMediaSample> &sample, int frameNb, OutputFrameData &data) -> bool;
auto WorkerProc() -> void;
auto GarbageCollect(int srcFrameNb) -> void;
Expand Down

0 comments on commit 391466f

Please sign in to comment.