From cd48c8ab6d7425d4b4d9fdb2493da69b44848c9e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 25 Feb 2018 13:45:43 +0100 Subject: [PATCH] Implement HTMLInput::set_url(). --- cef_capture.cpp | 41 +++++++++++++++++++++++++++++++++-------- cef_capture.h | 10 ++++++++++ nageru_cef_app.h | 4 ++-- theme.cpp | 12 +++++++++++- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/cef_capture.cpp b/cef_capture.cpp index c05c295..a2f230a 100644 --- a/cef_capture.cpp +++ b/cef_capture.cpp @@ -39,6 +39,23 @@ CEFCapture::~CEFCapture() } } +void CEFCapture::post_to_cef_ui_thread(std::function &&func) +{ + lock_guard lock(browser_mutex); + if (browser != nullptr) { + CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func))); + } else { + deferred_tasks.push_back(std::move(func)); + } +} + +void CEFCapture::set_url(const string &url) +{ + post_to_cef_ui_thread([this, url] { + browser->GetMainFrame()->LoadURL(url); + }); +} + void CEFCapture::OnPaint(const void *buffer, int width, int height) { steady_clock::time_point timestamp = steady_clock::now(); @@ -79,14 +96,22 @@ void CEFCapture::start_bm_capture() { cef_app->initialize_cef(); - 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; - - CefWindowInfo window_info; - window_info.SetAsWindowless(0); - CefBrowserHost::CreateBrowser(window_info, cef_client, start_url, browser_settings, nullptr); + CefPostTask(TID_UI, new CEFTaskAdapter([this]{ + lock_guard lock(browser_mutex); + + 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; + + CefWindowInfo window_info; + window_info.SetAsWindowless(0); + browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr); + for (function &task : deferred_tasks) { + task(); + } + deferred_tasks.clear(); + })); } void CEFCapture::stop_dequeue_thread() diff --git a/cef_capture.h b/cef_capture.h index dc74b30..8dc053d 100644 --- a/cef_capture.h +++ b/cef_capture.h @@ -69,6 +69,8 @@ public: return card_index; } + void set_url(const std::string &url); + void OnPaint(const void *buffer, int width, int height); // CaptureInterface. @@ -145,6 +147,8 @@ public: uint32_t get_current_audio_input() const override { return 0; } private: + void post_to_cef_ui_thread(std::function &&func); + CefRefPtr cef_client; unsigned width, height; int card_index = -1; @@ -159,6 +163,12 @@ private: std::string description, start_url; + std::mutex browser_mutex; + CefRefPtr browser; // Under . + + // Tasks waiting for to get ready. Under . + std::vector> deferred_tasks; + int timecode = 0; }; diff --git a/nageru_cef_app.h b/nageru_cef_app.h index dff9844..a120d5a 100644 --- a/nageru_cef_app.h +++ b/nageru_cef_app.h @@ -33,8 +33,8 @@ class CEFTaskAdapter : public CefTask { public: - CEFTaskAdapter(const std::function& func) - : func(func) {} + CEFTaskAdapter(const std::function&& func) + : func(std::move(func)) {} void Execute() override { func(); } private: diff --git a/theme.cpp b/theme.cpp index d43cc32..bc57b0d 100644 --- a/theme.cpp +++ b/theme.cpp @@ -422,6 +422,15 @@ int HTMLInput_new(lua_State* L) return ret; } +int HTMLInput_set_url(lua_State* L) +{ + assert(lua_gettop(L) == 2); + CEFCapture **video_input = (CEFCapture **)luaL_checkudata(L, 1, "HTMLInput"); + string new_url = checkstdstring(L, 2); + (*video_input)->set_url(new_url); + return 0; +} + int HTMLInput_get_signal_num(lua_State* L) { assert(lua_gettop(L) == 1); @@ -639,8 +648,9 @@ const luaL_Reg VideoInput_funcs[] = { }; const luaL_Reg HTMLInput_funcs[] = { - // TODO: reload, set_url, execute_javascript, perhaps change_framerate? + // TODO: reload, execute_javascript, perhaps set_max_fps? { "new", HTMLInput_new }, + { "set_url", HTMLInput_set_url }, { "get_signal_num", HTMLInput_get_signal_num }, { NULL, NULL } }; -- 2.39.2