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(width, height, 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)
44 lock_guard<mutex> lock(browser_mutex);
45 if (browser != nullptr) {
46 CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func)));
48 deferred_tasks.push_back(std::move(func));
52 void CEFCapture::set_url(const string &url)
54 post_to_cef_ui_thread([this, url] {
55 browser->GetMainFrame()->LoadURL(url);
59 void CEFCapture::OnPaint(const void *buffer, int width, int height)
61 steady_clock::time_point timestamp = steady_clock::now();
63 VideoFormat video_format;
64 video_format.width = width;
65 video_format.height = height;
66 video_format.stride = width * 4;
67 video_format.frame_rate_nom = 60; // FIXME
68 video_format.frame_rate_den = 1;
69 video_format.has_signal = true;
70 video_format.is_connected = true;
72 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
73 if (video_frame.data != nullptr) {
74 assert(video_frame.size >= unsigned(width * height * 4));
75 assert(!video_frame.interleaved);
76 memcpy(video_frame.data, buffer, width * height * 4);
77 video_frame.len = video_format.stride * height;
78 video_frame.received_timestamp = timestamp;
80 frame_callback(timecode++,
81 video_frame, 0, video_format,
82 FrameAllocator::Frame(), 0, AudioFormat());
85 #define FRAME_SIZE (8 << 20) // 8 MB.
87 void CEFCapture::configure_card()
89 if (video_frame_allocator == nullptr) {
90 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
91 set_video_frame_allocator(owned_video_frame_allocator.get());
95 void CEFCapture::start_bm_capture()
97 cef_app->initialize_cef();
99 CefPostTask(TID_UI, new CEFTaskAdapter([this]{
100 lock_guard<mutex> lock(browser_mutex);
102 CefBrowserSettings browser_settings;
103 browser_settings.web_security = cef_state_t::STATE_DISABLED;
104 browser_settings.webgl = cef_state_t::STATE_ENABLED;
105 browser_settings.windowless_frame_rate = 60;
107 CefWindowInfo window_info;
108 window_info.SetAsWindowless(0);
109 browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr);
110 for (function<void()> &task : deferred_tasks) {
113 deferred_tasks.clear();
117 void CEFCapture::stop_dequeue_thread()
119 lock_guard<mutex> lock(browser_mutex);
120 cef_app->close_browser(browser);
121 browser = nullptr; // Or unref_cef() will be sad.
122 cef_app->unref_cef();
125 std::map<uint32_t, VideoMode> CEFCapture::get_available_video_modes() const
130 snprintf(buf, sizeof(buf), "%ux%u", width, height);
133 mode.autodetect = false;
135 mode.height = height;
136 mode.frame_rate_num = 60; // FIXME
137 mode.frame_rate_den = 1;
138 mode.interlaced = false;
140 return {{ 0, mode }};
143 std::map<uint32_t, std::string> CEFCapture::get_available_video_inputs() const
145 return {{ 0, "HTML video input" }};
148 std::map<uint32_t, std::string> CEFCapture::get_available_audio_inputs() const
150 return {{ 0, "Fake HTML audio input (silence)" }};
153 void CEFCapture::set_video_mode(uint32_t video_mode_id)
155 assert(video_mode_id == 0);
158 void CEFCapture::set_video_input(uint32_t video_input_id)
160 assert(video_input_id == 0);
163 void CEFCapture::set_audio_input(uint32_t audio_input_id)
165 assert(audio_input_id == 0);
168 void NageruCEFClient::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
170 parent->OnPaint(buffer, width, height);
173 bool NageruCEFClient::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
175 rect = CefRect(0, 0, width, height);