]> git.sesse.net Git - nageru/blobdiff - cef_capture.cpp
When asked to execute JavaScript, defer until the current page load is done.
[nageru] / cef_capture.cpp
index a2f230aae9af610ec6710278b33f1ccbbe462472..76c5c7f753c8630ff6a796de58ac660daff65bf1 100644 (file)
@@ -52,10 +52,39 @@ void CEFCapture::post_to_cef_ui_thread(std::function<void()> &&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] {
+               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("<theme eval>");
+                       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();
@@ -64,7 +93,7 @@ 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;
@@ -82,6 +111,19 @@ void CEFCapture::OnPaint(const void *buffer, int width, int height)
                FrameAllocator::Frame(), 0, AudioFormat());
 }
 
+void CEFCapture::OnLoadEnd()
+{
+       post_to_cef_ui_thread([this] {
+               loaded = true;
+               for (const string &js : deferred_javascript) {
+                       CefString script_url("<theme eval>");
+                       int start_line = 1;
+                       browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
+               }
+               deferred_javascript.clear();
+       });
+}
+
 #define FRAME_SIZE (8 << 20)  // 8 MB.
 
 void CEFCapture::configure_card()
@@ -102,7 +144,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);
@@ -133,7 +175,7 @@ std::map<uint32_t, VideoMode> 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;
 
@@ -175,3 +217,8 @@ bool NageruCEFClient::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
        rect = CefRect(0, 0, width, height);
        return true;
 }
+
+void NageruCEFClient::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
+{
+       parent->OnLoadEnd();
+}