]> git.sesse.net Git - nageru/blobdiff - cef_capture.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / cef_capture.cpp
index 1836eb88f9cf474b9cc5c4287b3b7c011f39ac4c..c52e3a45ddccf92f18b6fb489a80d26b66f05ebc 100644 (file)
@@ -52,6 +52,7 @@ 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);
        });
 }
@@ -74,9 +75,13 @@ void CEFCapture::set_max_fps(int max_fps)
 void CEFCapture::execute_javascript_async(const string &js)
 {
        post_to_cef_ui_thread([this, js] {
-               CefString script_url("<theme eval>");
-               int start_line = 1;
-               browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
+               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);
+               }
        });
 }
 
@@ -94,16 +99,38 @@ void CEFCapture::OnPaint(const void *buffer, int width, int height)
        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("<theme eval>");
+                       int start_line = 1;
+                       browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
+               }
+               deferred_javascript.clear();
+       });
 }
 
 #define FRAME_SIZE (8 << 20)  // 8 MB.
@@ -199,3 +226,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();
+}