]> git.sesse.net Git - nageru/blob - cef_capture.cpp
ff978f288640d8036cb33e1479b5e5c39c6a07d8
[nageru] / cef_capture.cpp
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <chrono>
5 #include <memory>
6 #include <string>
7
8 #include "cef_capture.h"
9 #include "nageru_cef_app.h"
10
11 #undef CHECK
12 #include <cef_app.h>
13 #include <cef_browser.h>
14 #include <cef_client.h>
15
16 #include "bmusb/bmusb.h"
17
18 using namespace std;
19 using namespace std::chrono;
20 using namespace bmusb;
21
22 extern CefRefPtr<NageruCefApp> cef_app;
23
24 CEFCapture::CEFCapture(const string &url, unsigned width, unsigned height)
25         : cef_client(new NageruCEFClient(width, height, this)),
26           width(width),
27           height(height),
28           start_url(url)
29 {
30         char buf[256];
31         snprintf(buf, sizeof(buf), "CEF card %d", card_index + 1);
32         description = buf;
33 }
34
35 CEFCapture::~CEFCapture()
36 {
37         if (has_dequeue_callbacks) {
38                 dequeue_cleanup_callback();
39         }
40 }
41
42 void CEFCapture::post_to_cef_ui_thread(std::function<void()> &&func)
43 {
44         lock_guard<recursive_mutex> lock(browser_mutex);
45         if (browser != nullptr) {
46                 CefPostTask(TID_UI, new CEFTaskAdapter(std::move(func)));
47         } else {
48                 deferred_tasks.push_back(std::move(func));
49         }
50 }
51
52 void CEFCapture::set_url(const string &url)
53 {
54         post_to_cef_ui_thread([this, url] {
55                 loaded = false;
56                 browser->GetMainFrame()->LoadURL(url);
57         });
58 }
59
60 void CEFCapture::reload()
61 {
62         post_to_cef_ui_thread([this] {
63                 loaded = false;
64                 browser->Reload();
65         });
66 }
67
68 void CEFCapture::set_max_fps(int max_fps)
69 {
70         post_to_cef_ui_thread([this, max_fps] {
71                 browser->GetHost()->SetWindowlessFrameRate(max_fps);
72                 this->max_fps = max_fps;
73         });
74 }
75
76 void CEFCapture::execute_javascript_async(const string &js)
77 {
78         post_to_cef_ui_thread([this, js] {
79                 if (loaded) {
80                         CefString script_url("<theme eval>");
81                         int start_line = 1;
82                         browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
83                 } else {
84                         deferred_javascript.push_back(js);
85                 }
86         });
87 }
88
89 void CEFCapture::resize(unsigned width, unsigned height)
90 {
91         lock_guard<mutex> lock(resolution_mutex);
92         this->width = width;
93         this->height = height;
94 }
95
96 void CEFCapture::OnPaint(const void *buffer, int width, int height)
97 {
98         steady_clock::time_point timestamp = steady_clock::now();
99
100         VideoFormat video_format;
101         video_format.width = width;
102         video_format.height = height;
103         video_format.stride = width * 4;
104         video_format.frame_rate_nom = max_fps;
105         video_format.frame_rate_den = 1;
106         video_format.has_signal = true;
107         video_format.is_connected = true;
108
109         FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
110         if (video_frame.data == nullptr) {
111                 // We lost a frame, so we need to invalidate the entire thing.
112                 // (CEF only sends OnPaint when there are actual changes,
113                 // so we need to do this explicitly, or we could be stuck on an
114                 // old frame forever if the image doesn't change.)
115                 post_to_cef_ui_thread([this] {
116                         lock_guard<recursive_mutex> lock(browser_mutex);
117                         if (browser != nullptr) {  // Could happen if we are shutting down.
118                                 browser->GetHost()->Invalidate(PET_VIEW);
119                         }
120                 });
121                 ++timecode;
122         } else {
123                 assert(video_frame.size >= unsigned(width * height * 4));
124                 assert(!video_frame.interleaved);
125                 memcpy(video_frame.data, buffer, width * height * 4);
126                 video_frame.len = video_format.stride * height;
127                 video_frame.received_timestamp = timestamp;
128                 frame_callback(timecode++,
129                         video_frame, 0, video_format,
130                         FrameAllocator::Frame(), 0, AudioFormat());
131         }
132 }
133
134 void CEFCapture::OnLoadEnd()
135 {
136         post_to_cef_ui_thread([this] {
137                 loaded = true;
138                 for (const string &js : deferred_javascript) {
139                         CefString script_url("<theme eval>");
140                         int start_line = 1;
141                         browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
142                 }
143                 deferred_javascript.clear();
144         });
145 }
146
147 #define FRAME_SIZE (8 << 20)  // 8 MB.
148
149 void CEFCapture::configure_card()
150 {
151         if (video_frame_allocator == nullptr) {
152                 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
153                 set_video_frame_allocator(owned_video_frame_allocator.get());
154         }
155 }
156
157 void CEFCapture::start_bm_capture()
158 {
159         cef_app->initialize_cef();
160
161         CefPostTask(TID_UI, new CEFTaskAdapter([this]{
162                 lock_guard<recursive_mutex> lock(browser_mutex);
163
164                 CefBrowserSettings browser_settings;
165                 browser_settings.web_security = cef_state_t::STATE_DISABLED;
166                 browser_settings.webgl = cef_state_t::STATE_ENABLED;
167                 browser_settings.windowless_frame_rate = max_fps;
168
169                 CefWindowInfo window_info;
170                 window_info.SetAsWindowless(0);
171                 browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr);
172                 for (function<void()> &task : deferred_tasks) {
173                         task();
174                 }
175                 deferred_tasks.clear();
176         }));
177 }
178
179 void CEFCapture::stop_dequeue_thread()
180 {
181         {
182                 lock_guard<recursive_mutex> lock(browser_mutex);
183                 cef_app->close_browser(browser);
184                 browser = nullptr;  // Or unref_cef() will be sad.
185         }
186         cef_app->unref_cef();
187 }
188
189 std::map<uint32_t, VideoMode> CEFCapture::get_available_video_modes() const
190 {
191         VideoMode mode;
192
193         char buf[256];
194         snprintf(buf, sizeof(buf), "%ux%u", width, height);
195         mode.name = buf;
196
197         mode.autodetect = false;
198         mode.width = width;
199         mode.height = height;
200         mode.frame_rate_num = max_fps;
201         mode.frame_rate_den = 1;
202         mode.interlaced = false;
203
204         return {{ 0, mode }};
205 }
206
207 std::map<uint32_t, std::string> CEFCapture::get_available_video_inputs() const
208 {
209         return {{ 0, "HTML video input" }};
210 }
211
212 std::map<uint32_t, std::string> CEFCapture::get_available_audio_inputs() const
213 {
214         return {{ 0, "Fake HTML audio input (silence)" }};
215 }
216
217 void CEFCapture::set_video_mode(uint32_t video_mode_id)
218 {
219         assert(video_mode_id == 0);
220 }
221
222 void CEFCapture::set_video_input(uint32_t video_input_id)
223 {
224         assert(video_input_id == 0);
225 }
226
227 void CEFCapture::set_audio_input(uint32_t audio_input_id)
228 {
229         assert(audio_input_id == 0);
230 }
231
232 void NageruCEFClient::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
233 {
234         parent->OnPaint(buffer, width, height);
235 }
236
237 bool NageruCEFClient::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
238 {
239         return parent->GetViewRect(rect);
240 }
241
242 bool CEFCapture::GetViewRect(CefRect &rect)
243 {
244         lock_guard<mutex> lock(resolution_mutex);
245         rect = CefRect(0, 0, width, height);
246         return true;
247 }
248
249 void NageruCEFClient::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
250 {
251         parent->OnLoadEnd();
252 }