Skip to content

Commit

Permalink
Stop frame handler and remote control at the filter's destruction tim…
Browse files Browse the repository at this point in the history
…e, instead of Inactive() time, fix issue 54
  • Loading branch information
CrendKing committed Jun 17, 2021
1 parent 391466f commit a1a6f69
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 65 deletions.
25 changes: 0 additions & 25 deletions avisynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,6 @@ auto FrameHandler::EndFlush(const std::function<void ()> &interim) -> void {
Environment::GetInstance().Log(L"FrameHandler finish EndFlush()");
}

auto FrameHandler::Start() -> void {
_workerThread = std::thread(&FrameHandler::WorkerProc, this);
}

auto FrameHandler::Stop() -> void {
_isStopping = true;

BeginFlush();
EndFlush([]() -> void {
/*
* Stop the script after worker threads are paused and before flushing is done so that no new frame request (GetSourceFrame()) happens.
* And since _isFlushing is still on, existing frame request should also just drain instead of block.
*
* If no stop here, since AddInputSample() no longer adds frame, existing GetSourceFrame() calls will stuck forever.
*/
MainFrameServer::GetInstance().StopScript();
});

if (_workerThread.joinable()) {
_workerThread.join();
}

_isStopping = false;
}

auto FrameHandler::ResetInput() -> void {
_nextSourceFrameNb = 0;
_maxRequestedFrameNb = 0;
Expand Down
1 change: 1 addition & 0 deletions avisynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CSynthFilter;
class FrameHandler {
public:
explicit FrameHandler(CSynthFilter &filter);
~FrameHandler();

DISABLE_COPYING(FrameHandler)

Expand Down
5 changes: 2 additions & 3 deletions filter_common/src/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ CSynthFilter::CSynthFilter(LPUNKNOWN pUnk, HRESULT *phr)
CSynthFilter::~CSynthFilter() {
Environment::GetInstance().Log(L"Destroy CSynthFilter: %p", this);

_remoteControl.reset();
frameHandler.reset();

_numFilterInstances -= 1;
if (_numFilterInstances == 0) {
_remoteControl.reset();
frameHandler.reset();
AuxFrameServer::Destroy();
MainFrameServer::Destroy();
FrameServerCommon::Destroy();
Expand Down
28 changes: 28 additions & 0 deletions filter_common/src/frame_handler_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ FrameHandler::FrameHandler(CSynthFilter &filter)
ResetInput();
}

FrameHandler::~FrameHandler() {
if (_workerThread.joinable()) {
_isStopping = true;
Stop();
_workerThread.join();
}
}

auto FrameHandler::Start() -> void {
if (!_workerThread.joinable()) {
_isStopping = false;
_workerThread = std::thread(&FrameHandler::WorkerProc, this);
}
}

auto FrameHandler::Stop() -> void {
BeginFlush();
EndFlush([]() -> void {
/*
* Stop the script after worker threads are paused and before flushing is done so that no new frame request (GetSourceFrame()) happens.
* And since _isFlushing is still on, existing frame request should also just drain instead of block.
*
* If no stop here, since AddInputSample() no longer adds frame, existing GetSourceFrame() calls will stuck forever.
*/
MainFrameServer::GetInstance().StopScript();
});
}

auto FrameHandler::GetInputBufferSize() const -> int {
const std::shared_lock sharedSourceLock(_sourceMutex);

Expand Down
1 change: 0 additions & 1 deletion filter_common/src/input_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ auto CSynthFilterInputPin::Active() -> HRESULT {
}

auto CSynthFilterInputPin::Inactive() -> HRESULT {
_filter._remoteControl->Stop();
_filter.frameHandler->Stop();

return __super::Inactive();
Expand Down
16 changes: 5 additions & 11 deletions filter_common/src/remote_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@ RemoteControl::RemoteControl(CSynthFilter &filter)
}

RemoteControl::~RemoteControl() {
Stop();
}

auto RemoteControl::Start() -> void {
if (!_msgThread.joinable()) {
_msgThread = std::thread(&RemoteControl::Run, this);
}
}

auto RemoteControl::Stop() -> void {
if (_hWnd != nullptr) {
PostMessageW(_hWnd, WM_CLOSE, 0, 0);
}

if (_msgThread.joinable()) {
_msgThread.join();
}
}

_hWnd = nullptr;
auto RemoteControl::Start() -> void {
if (!_msgThread.joinable()) {
_msgThread = std::thread(&RemoteControl::Run, this);
}
}

auto RemoteControl::IsRunning() const -> bool {
Expand Down
1 change: 0 additions & 1 deletion filter_common/src/remote_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class RemoteControl {
DISABLE_COPYING(RemoteControl)

auto Start() -> void;
auto Stop() -> void;
auto IsRunning() const -> bool;

private:
Expand Down
24 changes: 0 additions & 24 deletions vapoursynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,6 @@ auto FrameHandler::EndFlush(const std::function<void ()> &interim) -> void {
Environment::GetInstance().Log(L"FrameHandler finish EndFlush()");
}

auto FrameHandler::Start() -> void {
_isStopping = false;
_workerThread = std::thread(&FrameHandler::WorkerProc, this);
}

auto FrameHandler::Stop() -> void {
_isStopping = true;

BeginFlush();
EndFlush([]() -> void {
/*
* Stop the script after worker threads are paused and before flushing is done so that no new frame request (GetSourceFrame()) happens.
* And since _isFlushing is still on, existing frame request should also just drain instead of block.
*
* If no stop here, since AddInputSample() no longer adds frame, existing GetSourceFrame() calls will stuck forever.
*/
MainFrameServer::GetInstance().StopScript();
});

if (_workerThread.joinable()) {
_workerThread.join();
}
}

FrameHandler::SourceFrameInfo::~SourceFrameInfo() {
AVSF_VS_API->freeFrame(frame);
}
Expand Down
1 change: 1 addition & 0 deletions vapoursynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CSynthFilter;
class FrameHandler {
public:
explicit FrameHandler(CSynthFilter &filter);
~FrameHandler();

DISABLE_COPYING(FrameHandler)

Expand Down

0 comments on commit a1a6f69

Please sign in to comment.