X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=cef_capture.cpp;h=f692c857f0201b1800a7cd913fdb40af3b701fbb;hb=b9feb66845bf24465b7671ac9ff7a52b88f6032b;hp=9dc454ba33d426a6abfcb86234c36e193f221c66;hpb=4dcab181655d215ad0105f8adfe76dd6fd8bd201;p=nageru diff --git a/cef_capture.cpp b/cef_capture.cpp index 9dc454b..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); + 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)); } @@ -86,6 +90,26 @@ void CEFCapture::execute_javascript_async(const string &js) }); } +void CEFCapture::resize(unsigned width, unsigned height) +{ + lock_guard lock(resolution_mutex); + this->width = width; + 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(); @@ -105,9 +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] { - browser->GetHost()->Invalidate(PET_VIEW); - }); + request_new_frame(); ++timecode; } else { assert(video_frame.size >= unsigned(width * height * 4)); @@ -149,7 +171,7 @@ void CEFCapture::start_bm_capture() cef_app->initialize_cef(); CefPostTask(TID_UI, new CEFTaskAdapter([this]{ - lock_guard lock(browser_mutex); + lock_guard lock(browser_mutex); CefBrowserSettings browser_settings; browser_settings.web_security = cef_state_t::STATE_DISABLED; @@ -168,9 +190,11 @@ void CEFCapture::start_bm_capture() void CEFCapture::stop_dequeue_thread() { - lock_guard lock(browser_mutex); - cef_app->close_browser(browser); - browser = nullptr; // Or unref_cef() will be sad. + { + lock_guard lock(browser_mutex); + cef_app->close_browser(browser); + browser = nullptr; // Or unref_cef() will be sad. + } cef_app->unref_cef(); } @@ -224,6 +248,12 @@ void NageruCEFClient::OnPaint(CefRefPtr browser, PaintElementType ty bool NageruCEFClient::GetViewRect(CefRefPtr browser, CefRect &rect) { + return parent->GetViewRect(rect); +} + +bool CEFCapture::GetViewRect(CefRect &rect) +{ + lock_guard lock(resolution_mutex); rect = CefRect(0, 0, width, height); return true; }