Skip to content

Commit

Permalink
Don't send frame to encoder if capture fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nowrep committed Aug 6, 2023
1 parent 03b7552 commit 959d074
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ struct wf_buffer
timespec presented;
uint64_t base_usec;

bool capture_success;

std::atomic<bool> released{true}; // if the buffer can be used to store new pending frames
std::atomic<bool> available{false}; // if the buffer can be used to feed the encoder
};
Expand Down Expand Up @@ -247,11 +249,15 @@ static void frame_handle_ready(void *, struct zwlr_screencopy_frame_v1 *,
buffer_copy_done = true;
buffer.presented.tv_sec = ((1ll * tv_sec_hi) << 32ll) | tv_sec_low;
buffer.presented.tv_nsec = tv_nsec;
buffer.capture_success = true;
frame_failed_cnt = 0;
}

static void frame_handle_failed(void *, struct zwlr_screencopy_frame_v1 *) {
std::cerr << "Failed to copy frame, retrying..." << std::endl;

auto& buffer = buffers[active_buffer];
buffer.capture_success = false;
buffer_copy_done = true;
++frame_failed_cnt;
if (frame_failed_cnt > MAX_FRAME_FAILURES)
Expand Down Expand Up @@ -494,6 +500,14 @@ static void write_loop(FrameWriterParams params)
}
auto& buffer = buffers[last_encoded_frame];

if (!buffer.capture_success)
{
buffer.available = false;
buffer.released = true;
last_encoded_frame = next_frame(last_encoded_frame);
continue;
}

frame_writer_pending_mutex.lock();
frame_writer_mutex.lock();
frame_writer_pending_mutex.unlock();
Expand Down Expand Up @@ -543,12 +557,12 @@ static void write_loop(FrameWriterParams params)
buffer.base_usec, buffer.y_invert);
}

frame_writer_mutex.unlock();

if (!do_cont) {
break;
}

frame_writer_mutex.unlock();

buffer.available = false;
buffer.released = true;

Expand Down

0 comments on commit 959d074

Please sign in to comment.