#include <set>
#include <string>
#include <thread>
+#include <vector>
#undef CHECK
#include <cef_client.h>
// 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)
- : width(width), height(height), parent(parent) {}
+ NageruCEFClient(CEFCapture *parent)
+ : parent(parent) {}
CefRefPtr<CefRenderHandler> GetRenderHandler() override
{
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);
+ bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) override;
+
+ // CefLoadHandler.
+
+ void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
private:
- int width, height;
CEFCapture *parent;
IMPLEMENT_REFCOUNTING(NageruCEFClient);
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 resize(unsigned width, unsigned height);
+ void request_new_frame();
+ // Callbacks from NageruCEFClient.
void OnPaint(const void *buffer, int width, int height);
+ bool GetViewRect(CefRect &rect);
+ void OnLoadEnd();
// CaptureInterface.
void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
}
- bmusb::PixelFormat get_current_pixel_format() const
+ bmusb::PixelFormat get_current_pixel_format() const override
{
return bmusb::PixelFormat_8BitBGRA;
}
uint32_t get_current_audio_input() const override { return 0; }
private:
- void post_to_cef_ui_thread(std::function<void()> &&func);
+ void post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms = 0);
CefRefPtr<NageruCEFClient> cef_client;
- unsigned width, height;
+
+ // Needs to be different from browser_mutex below, since GetViewRect
+ // can be called unpredictably from when we are already holding
+ // <browser_mutex>.
+ std::mutex resolution_mutex;
+ unsigned width, height; // Under <resolution_mutex>.
+
int card_index = -1;
bool has_dequeue_callbacks = false;
bmusb::frame_callback_t frame_callback = nullptr;
std::string description, start_url;
+ std::atomic<int> max_fps{60};
- std::mutex browser_mutex;
+ // Needs to be recursive because the lambda in OnPaint could cause
+ // OnPaint itself to be called.
+ std::recursive_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;
};