X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=cef_capture.cpp;h=c52e3a45ddccf92f18b6fb489a80d26b66f05ebc;hp=548e529ad3d2e5dc457e638288c787043d2016eb;hb=refs%2Fheads%2Fcef;hpb=8ec5630a8e7ec089d2e39697d51642278d1977d7 diff --git a/cef_capture.cpp b/cef_capture.cpp index 548e529..c52e3a4 100644 --- a/cef_capture.cpp +++ b/cef_capture.cpp @@ -52,6 +52,7 @@ void CEFCapture::post_to_cef_ui_thread(std::function &&func) void CEFCapture::set_url(const string &url) { post_to_cef_ui_thread([this, url] { + loaded = false; browser->GetMainFrame()->LoadURL(url); }); } @@ -63,6 +64,27 @@ void CEFCapture::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::OnPaint(const void *buffer, int width, int height) { steady_clock::time_point timestamp = steady_clock::now(); @@ -71,22 +93,44 @@ 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.) + post_to_cef_ui_thread([this] { + browser->GetHost()->Invalidate(PET_VIEW); + }); + ++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. @@ -109,7 +153,7 @@ void CEFCapture::start_bm_capture() 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; + browser_settings.windowless_frame_rate = max_fps; CefWindowInfo window_info; window_info.SetAsWindowless(0); @@ -140,7 +184,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; @@ -182,3 +226,8 @@ bool NageruCEFClient::GetViewRect(CefRefPtr browser, CefRect &rect) rect = CefRect(0, 0, width, height); return true; } + +void NageruCEFClient::OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) +{ + parent->OnLoadEnd(); +}