X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=cef_capture.cpp;h=f692c857f0201b1800a7cd913fdb40af3b701fbb;hp=ff978f288640d8036cb33e1479b5e5c39c6a07d8;hb=e284d1c7a2e18ee7e4aea082c4a57a3504a0f5e8;hpb=e7f9c69a5fe88a3fea5a7dc3ce71e7ed337bdbd7 diff --git a/cef_capture.cpp b/cef_capture.cpp index ff978f2..f692c85 100644 --- a/cef_capture.cpp +++ b/cef_capture.cpp @@ -39,11 +39,15 @@ CEFCapture::~CEFCapture() } } -void CEFCapture::post_to_cef_ui_thread(std::function &&func) +void CEFCapture::post_to_cef_ui_thread(std::function &&func, int64_t delay_ms) { lock_guard lock(browser_mutex); if (browser != nullptr) { - CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func))); + if (delay_ms <= 0) { + CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func))); + } else { + CefPostDelayedTask(TID_UI, new CEFTaskAdapter(std::move(func)), delay_ms); + } } else { deferred_tasks.push_back(std::move(func)); } @@ -93,6 +97,19 @@ void CEFCapture::resize(unsigned width, unsigned height) this->height = height; } +void CEFCapture::request_new_frame() +{ + // By adding a delay, we make sure we don't get a new frame + // delivered immediately (we probably already are on the UI thread), + // where we couldn't really deal with it. + post_to_cef_ui_thread([this] { + lock_guard lock(browser_mutex); + if (browser != nullptr) { // Could happen if we are shutting down. + browser->GetHost()->Invalidate(PET_VIEW); + } + }, 16); +} + void CEFCapture::OnPaint(const void *buffer, int width, int height) { steady_clock::time_point timestamp = steady_clock::now(); @@ -112,12 +129,7 @@ void CEFCapture::OnPaint(const void *buffer, int width, int height) // (CEF only sends OnPaint when there are actual changes, // so we need to do this explicitly, or we could be stuck on an // old frame forever if the image doesn't change.) - post_to_cef_ui_thread([this] { - lock_guard lock(browser_mutex); - if (browser != nullptr) { // Could happen if we are shutting down. - browser->GetHost()->Invalidate(PET_VIEW); - } - }); + request_new_frame(); ++timecode; } else { assert(video_frame.size >= unsigned(width * height * 4));