Skip to content

Commit

Permalink
Lock the source mutex before acquiring the condition variable
Browse files Browse the repository at this point in the history
  • Loading branch information
CrendKing committed Aug 3, 2024
1 parent 055194f commit 9e686b6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
32 changes: 18 additions & 14 deletions avisynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(_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<int>(_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");
Expand Down
32 changes: 18 additions & 14 deletions vapoursynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(_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<int>(_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");
Expand Down

0 comments on commit 9e686b6

Please sign in to comment.