X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=cef_capture.cpp;h=b6b8cca489ff782182541101a1db372ac1ab735a;hb=ffd68fbfb90242069af957f2a28908f0559f8348;hp=c05c2953deff7cc3837f1f203917eee8f734b843;hpb=b68d8a25951faf5b967b7a35fa0a363b4b68fbc0;p=nageru diff --git a/cef_capture.cpp b/cef_capture.cpp index c05c295..b6b8cca 100644 --- a/cef_capture.cpp +++ b/cef_capture.cpp @@ -22,7 +22,7 @@ using namespace bmusb; extern CefRefPtr cef_app; CEFCapture::CEFCapture(const string &url, unsigned width, unsigned height) - : cef_client(new NageruCEFClient(width, height, this)), + : cef_client(new NageruCEFClient(this)), width(width), height(height), start_url(url) @@ -39,6 +39,77 @@ CEFCapture::~CEFCapture() } } +void CEFCapture::post_to_cef_ui_thread(std::function &&func, int64_t delay_ms) +{ + lock_guard lock(browser_mutex); + if (browser != nullptr) { + 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)); + } +} + +void CEFCapture::set_url(const string &url) +{ + post_to_cef_ui_thread([this, url] { + loaded = false; + browser->GetMainFrame()->LoadURL(url); + }); +} + +void CEFCapture::reload() +{ + post_to_cef_ui_thread([this] { + loaded = false; + browser->Reload(); + }); +} + +void CEFCapture::set_max_fps(int max_fps) +{ + post_to_cef_ui_thread([this, max_fps] { + browser->GetHost()->SetWindowlessFrameRate(max_fps); + this->max_fps = max_fps; + }); +} + +void CEFCapture::execute_javascript_async(const string &js) +{ + post_to_cef_ui_thread([this, js] { + if (loaded) { + CefString script_url(""); + int start_line = 1; + browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line); + } else { + deferred_javascript.push_back(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(); @@ -47,22 +118,42 @@ void CEFCapture::OnPaint(const void *buffer, int width, int height) video_format.width = width; video_format.height = height; video_format.stride = width * 4; - video_format.frame_rate_nom = 60; // FIXME + video_format.frame_rate_nom = max_fps; video_format.frame_rate_den = 1; video_format.has_signal = true; video_format.is_connected = true; FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame(); - if (video_frame.data != nullptr) { + if (video_frame.data == nullptr) { + // We lost a frame, so we need to invalidate the entire thing. + // (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.) + request_new_frame(); + ++timecode; + } else { assert(video_frame.size >= unsigned(width * height * 4)); assert(!video_frame.interleaved); memcpy(video_frame.data, buffer, width * height * 4); video_frame.len = video_format.stride * height; video_frame.received_timestamp = timestamp; + frame_callback(timecode++, + video_frame, 0, video_format, + FrameAllocator::Frame(), 0, AudioFormat()); } - frame_callback(timecode++, - video_frame, 0, video_format, - FrameAllocator::Frame(), 0, AudioFormat()); +} + +void CEFCapture::OnLoadEnd() +{ + post_to_cef_ui_thread([this] { + loaded = true; + for (const string &js : deferred_javascript) { + CefString script_url(""); + int start_line = 1; + browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line); + } + deferred_javascript.clear(); + }); } #define FRAME_SIZE (8 << 20) // 8 MB. @@ -79,21 +170,31 @@ void CEFCapture::start_bm_capture() { cef_app->initialize_cef(); - CefBrowserSettings browser_settings; - browser_settings.web_security = cef_state_t::STATE_DISABLED; - browser_settings.webgl = cef_state_t::STATE_ENABLED; - browser_settings.windowless_frame_rate = 60; + CefPostTask(TID_UI, new CEFTaskAdapter([this]{ + lock_guard lock(browser_mutex); - CefWindowInfo window_info; - window_info.SetAsWindowless(0); - CefBrowserHost::CreateBrowser(window_info, cef_client, start_url, browser_settings, nullptr); + CefBrowserSettings browser_settings; + browser_settings.web_security = cef_state_t::STATE_DISABLED; + browser_settings.webgl = cef_state_t::STATE_ENABLED; + browser_settings.windowless_frame_rate = max_fps; + + CefWindowInfo window_info; + window_info.SetAsWindowless(0); + browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr); + for (function &task : deferred_tasks) { + task(); + } + deferred_tasks.clear(); + })); } 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(); } @@ -108,7 +209,7 @@ std::map CEFCapture::get_available_video_modes() const mode.autodetect = false; mode.width = width; mode.height = height; - mode.frame_rate_num = 60; // FIXME + mode.frame_rate_num = max_fps; mode.frame_rate_den = 1; mode.interlaced = false; @@ -147,6 +248,17 @@ 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; } + +void NageruCEFClient::OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) +{ + parent->OnLoadEnd(); +}