Skip to content

Commit

Permalink
Add timeout blocking acquire.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Oct 28, 2023
1 parent ba5f696 commit 261d8da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 20 additions & 7 deletions video/ffmpeg_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ struct VideoDecoder::Impl
double get_estimated_audio_playback_timestamp_raw();
void latch_audio_presentation_target(double pts);

bool acquire_video_frame(VideoFrame &frame);
bool acquire_video_frame(VideoFrame &frame, int timeout_ms);
int try_acquire_video_frame(VideoFrame &frame);
bool is_eof();
void release_video_frame(unsigned index, Vulkan::Semaphore sem);
Expand Down Expand Up @@ -2045,7 +2045,7 @@ int VideoDecoder::Impl::try_acquire_video_frame(VideoFrame &frame)
}
}

bool VideoDecoder::Impl::acquire_video_frame(VideoFrame &frame)
bool VideoDecoder::Impl::acquire_video_frame(VideoFrame &frame, int timeout_ms)
{
if (!decode_thread.joinable())
return false;
Expand All @@ -2058,10 +2058,23 @@ bool VideoDecoder::Impl::acquire_video_frame(VideoFrame &frame)
cond.notify_one();

int index = -1;

// Poll the video queue for new frames.
cond.wait(holder, [this, &index]() {
return (index = find_acquire_video_frame_locked()) >= 0 || acquire_is_eof || teardown;
});
if (timeout_ms >= 0)
{
auto target_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
bool success = cond.wait_until(holder, target_time, [this, &index]() {
return (index = find_acquire_video_frame_locked()) >= 0 || acquire_is_eof || teardown;
});
if (!success)
return false;
}
else
{
cond.wait(holder, [this, &index]() {
return (index = find_acquire_video_frame_locked()) >= 0 || acquire_is_eof || teardown;
});
}

if (index < 0)
return false;
Expand Down Expand Up @@ -2526,9 +2539,9 @@ double VideoDecoder::get_estimated_audio_playback_timestamp_raw()
return impl->get_estimated_audio_playback_timestamp_raw();
}

bool VideoDecoder::acquire_video_frame(VideoFrame &frame)
bool VideoDecoder::acquire_video_frame(VideoFrame &frame, int timeout_ms)
{
return impl->acquire_video_frame(frame);
return impl->acquire_video_frame(frame, timeout_ms);
}

int VideoDecoder::try_acquire_video_frame(VideoFrame &frame)
Expand Down
2 changes: 1 addition & 1 deletion video/ffmpeg_decode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class VideoDecoder

// Client is responsible for displaying the frame in due time.
// A video frame can be released when the returned PTS is out of date.
bool acquire_video_frame(VideoFrame &frame);
bool acquire_video_frame(VideoFrame &frame, int timeout_ms = -1);

// Poll acquire. Returns positive on success, 0 on no available image, negative number on EOF.
int try_acquire_video_frame(VideoFrame &frame);
Expand Down

0 comments on commit 261d8da

Please sign in to comment.