Skip to content

Commit

Permalink
[cscore] SourceImpl waits for any new frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Dec 22, 2024
1 parent 807dffe commit 23279e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
24 changes: 19 additions & 5 deletions cscore/src/main/native/cpp/SourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,34 @@ Frame SourceImpl::GetCurFrame() {

Frame SourceImpl::GetNextFrame() {
std::unique_lock lock{m_frameMutex};
auto oldTime = m_frame.GetTime();
m_frameCv.wait(
lock, [=, this] { return oldTime == 0 || m_frame.GetTime() != oldTime; });

// Wait unitl m_frame has a timestamp other than the last time we called
// GetNextFrame
m_frameCv.wait(lock, [=, this] {
return m_lastGetNextFrameTime == 0 ||
m_frame.GetTime() != m_lastGetNextFrameTime;
});

// Write down this frame's time (treating time as a frame uuid)
m_lastGetNextFrameTime = m_frame.GetTime();

return m_frame;
}

Frame SourceImpl::GetNextFrame(double timeout) {
std::unique_lock lock{m_frameMutex};
auto oldTime = m_frame.GetTime();

// Wait unitl m_frame has a timestamp other than the last time we called
// GetNextFrame
if (!m_frameCv.wait_for(
lock, std::chrono::milliseconds(static_cast<int>(timeout * 1000)),
[=, this] { return m_frame.GetTime() != oldTime; })) {
[=, this] { return m_frame.GetTime() != m_lastGetNextFrameTime; })) {
m_frame = Frame{*this, "timed out getting frame", wpi::Now()};
}

// Write down this frame's time (treating time as a frame uuid)
m_lastGetNextFrameTime = m_frame.GetTime();

return m_frame;
}

Expand Down
3 changes: 3 additions & 0 deletions cscore/src/main/native/cpp/SourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class SourceImpl : public PropertyContainer {
// MUST be located below m_poolMutex as the Frame destructor calls back
// into SourceImpl::ReleaseImage, which locks m_poolMutex.
Frame m_frame;

// The timestamp of the last image we handed on to a sink in GetNextFrame
Frame::Time m_lastGetNextFrameTime{0};
};

} // namespace cs
Expand Down

0 comments on commit 23279e0

Please sign in to comment.