8 #include "cef_capture.h"
9 #include "nageru_cef_app.h"
13 #include <cef_browser.h>
14 #include <cef_client.h>
16 #include "bmusb/bmusb.h"
19 using namespace std::chrono;
20 using namespace bmusb;
22 extern CefRefPtr<NageruCefApp> cef_app;
24 CEFCapture::CEFCapture(const string &url, unsigned width, unsigned height)
25 : cef_client(new NageruCEFClient(this)),
31 snprintf(buf, sizeof(buf), "CEF card %d", card_index + 1);
35 CEFCapture::~CEFCapture()
37 if (has_dequeue_callbacks) {
38 dequeue_cleanup_callback();
42 void CEFCapture::post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms)
44 lock_guard<recursive_mutex> lock(browser_mutex);
45 if (browser != nullptr) {
47 CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func)));
49 CefPostDelayedTask(TID_UI, new CEFTaskAdapter(std::move(func)), delay_ms);
52 deferred_tasks.push_back(std::move(func));
56 void CEFCapture::set_url(const string &url)
58 post_to_cef_ui_thread([this, url] {
60 browser->GetMainFrame()->LoadURL(url);
64 void CEFCapture::reload()
66 post_to_cef_ui_thread([this] {
72 void CEFCapture::set_max_fps(int max_fps)
74 post_to_cef_ui_thread([this, max_fps] {
75 browser->GetHost()->SetWindowlessFrameRate(max_fps);
76 this->max_fps = max_fps;
80 void CEFCapture::execute_javascript_async(const string &js)
82 post_to_cef_ui_thread([this, js] {
84 CefString script_url("<theme eval>");
86 browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
88 deferred_javascript.push_back(js);
93 void CEFCapture::resize(unsigned width, unsigned height)
95 lock_guard<mutex> lock(resolution_mutex);
97 this->height = height;
100 void CEFCapture::request_new_frame()
102 // By adding a delay, we make sure we don't get a new frame
103 // delivered immediately (we probably already are on the UI thread),
104 // where we couldn't really deal with it.
105 post_to_cef_ui_thread([this] {
106 lock_guard<recursive_mutex> lock(browser_mutex);
107 if (browser != nullptr) { // Could happen if we are shutting down.
108 browser->GetHost()->Invalidate(PET_VIEW);
113 void CEFCapture::OnPaint(const void *buffer, int width, int height)
115 steady_clock::time_point timestamp = steady_clock::now();
117 VideoFormat video_format;
118 video_format.width = width;
119 video_format.height = height;
120 video_format.stride = width * 4;
121 video_format.frame_rate_nom = max_fps;
122 video_format.frame_rate_den = 1;
123 video_format.has_signal = true;
124 video_format.is_connected = true;
126 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
127 if (video_frame.data == nullptr) {
128 // We lost a frame, so we need to invalidate the entire thing.
129 // (CEF only sends OnPaint when there are actual changes,
130 // so we need to do this explicitly, or we could be stuck on an
131 // old frame forever if the image doesn't change.)
135 assert(video_frame.size >= unsigned(width * height * 4));
136 assert(!video_frame.interleaved);
137 memcpy(video_frame.data, buffer, width * height * 4);
138 video_frame.len = video_format.stride * height;
139 video_frame.received_timestamp = timestamp;
140 frame_callback(timecode++,
141 video_frame, 0, video_format,
142 FrameAllocator::Frame(), 0, AudioFormat());
146 void CEFCapture::OnLoadEnd()
148 post_to_cef_ui_thread([this] {
150 for (const string &js : deferred_javascript) {
151 CefString script_url("<theme eval>");
153 browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
155 deferred_javascript.clear();
159 #define FRAME_SIZE (8 << 20) // 8 MB.
161 void CEFCapture::configure_card()
163 if (video_frame_allocator == nullptr) {
164 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
165 set_video_frame_allocator(owned_video_frame_allocator.get());
169 void CEFCapture::start_bm_capture()
171 cef_app->initialize_cef();
173 CefPostTask(TID_UI, new CEFTaskAdapter([this]{
174 lock_guard<recursive_mutex> lock(browser_mutex);
176 CefBrowserSettings browser_settings;
177 browser_settings.web_security = cef_state_t::STATE_DISABLED;
178 browser_settings.webgl = cef_state_t::STATE_ENABLED;
179 browser_settings.windowless_frame_rate = max_fps;
181 CefWindowInfo window_info;
182 window_info.SetAsWindowless(0);
183 browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr);
184 for (function<void()> &task : deferred_tasks) {
187 deferred_tasks.clear();
191 void CEFCapture::stop_dequeue_thread()
194 lock_guard<recursive_mutex> lock(browser_mutex);
195 cef_app->close_browser(browser);
196 browser = nullptr; // Or unref_cef() will be sad.
198 cef_app->unref_cef();
201 std::map<uint32_t, VideoMode> CEFCapture::get_available_video_modes() const
206 snprintf(buf, sizeof(buf), "%ux%u", width, height);
209 mode.autodetect = false;
211 mode.height = height;
212 mode.frame_rate_num = max_fps;
213 mode.frame_rate_den = 1;
214 mode.interlaced = false;
216 return {{ 0, mode }};
219 std::map<uint32_t, std::string> CEFCapture::get_available_video_inputs() const
221 return {{ 0, "HTML video input" }};
224 std::map<uint32_t, std::string> CEFCapture::get_available_audio_inputs() const
226 return {{ 0, "Fake HTML audio input (silence)" }};
229 void CEFCapture::set_video_mode(uint32_t video_mode_id)
231 assert(video_mode_id == 0);
234 void CEFCapture::set_video_input(uint32_t video_input_id)
236 assert(video_input_id == 0);
239 void CEFCapture::set_audio_input(uint32_t audio_input_id)
241 assert(audio_input_id == 0);
244 void NageruCEFClient::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
246 parent->OnPaint(buffer, width, height);
249 void NageruCEFClient::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
251 parent->GetViewRect(rect);
254 void CEFCapture::GetViewRect(CefRect &rect)
256 lock_guard<mutex> lock(resolution_mutex);
257 rect = CefRect(0, 0, width, height);
260 void NageruCEFClient::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)