]> git.sesse.net Git - nageru/blobdiff - cef_capture.h
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / cef_capture.h
index dc74b30d527c4c28d6d54cb2ec1431ed77039fde..c497179d72e127077e4abb193e34f720ed7ea141 100644 (file)
@@ -17,6 +17,7 @@
 #include <set>
 #include <string>
 #include <thread>
+#include <vector>
 
 #undef CHECK
 #include <cef_client.h>
@@ -31,7 +32,7 @@ class CEFCapture;
 
 // A helper class for CEFCapture to proxy information to CEF, without becoming
 // CEF-refcounted itself.
-class NageruCEFClient : public CefClient, public CefRenderHandler
+class NageruCEFClient : public CefClient, public CefRenderHandler, public CefLoadHandler
 {
 public:
        NageruCEFClient(int width, int height, CEFCapture *parent)
@@ -42,10 +43,21 @@ public:
                return this;
        }
 
+       CefRefPtr<CefLoadHandler> GetLoadHandler() override
+       {
+               return this;
+       }
+
+       // CefRenderHandler.
+
        void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override;
 
        bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect);
 
+       // CefLoadHandler.
+
+       void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
+
 private:
        int width, height;
        CEFCapture *parent;
@@ -69,8 +81,15 @@ public:
                return card_index;
        }
 
+       void set_url(const std::string &url);
+       void reload();
+       void set_max_fps(int max_fps);
+       void execute_javascript_async(const std::string &js);
+
        void OnPaint(const void *buffer, int width, int height);
 
+       void OnLoadEnd();
+
        // CaptureInterface.
        void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
        {
@@ -145,6 +164,8 @@ public:
        uint32_t get_current_audio_input() const override { return 0; }
 
 private:
+       void post_to_cef_ui_thread(std::function<void()> &&func);
+
        CefRefPtr<NageruCEFClient> cef_client;
        unsigned width, height;
        int card_index = -1;
@@ -158,6 +179,21 @@ private:
        bmusb::frame_callback_t frame_callback = nullptr;
 
        std::string description, start_url;
+       std::atomic<int> max_fps{60};
+
+       std::mutex browser_mutex;
+       CefRefPtr<CefBrowser> browser;  // Under <browser_mutex>.
+
+       // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
+       std::vector<std::function<void()>> deferred_tasks;
+
+       // Whether the last set_url() (includes the implicit one in the constructor)
+       // has loaded yet. Accessed from the CEF thread only.
+       bool loaded = false;
+
+       // JavaScript waiting for the first page (well, any page) to have loaded.
+       // Accessed from the CEF thread only.
+       std::vector<std::string> deferred_javascript;
 
        int timecode = 0;
 };