]> git.sesse.net Git - nageru/blob - cef_capture.cpp
Implement HTMLInput::execute_javascript_async().
[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<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                 browser->GetMainFrame()->LoadURL(url);
56         });
57 }
58
59 void CEFCapture::reload()
60 {
61         post_to_cef_ui_thread([this] {
62                 browser->Reload();
63         });
64 }
65
66 void CEFCapture::set_max_fps(int max_fps)
67 {
68         post_to_cef_ui_thread([this, max_fps] {
69                 browser->GetHost()->SetWindowlessFrameRate(max_fps);
70                 this->max_fps = max_fps;
71         });
72 }
73
74 void CEFCapture::execute_javascript_async(const string &js)
75 {
76         post_to_cef_ui_thread([this, js] {
77                 CefString script_url("<theme eval>");
78                 int start_line = 1;
79                 browser->GetMainFrame()->ExecuteJavaScript(js, script_url, start_line);
80         });
81 }
82
83 void CEFCapture::OnPaint(const void *buffer, int width, int height)
84 {
85         steady_clock::time_point timestamp = steady_clock::now();
86
87         VideoFormat video_format;
88         video_format.width = width;
89         video_format.height = height;
90         video_format.stride = width * 4;
91         video_format.frame_rate_nom = max_fps;
92         video_format.frame_rate_den = 1;
93         video_format.has_signal = true;
94         video_format.is_connected = true;
95
96         FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
97         if (video_frame.data != nullptr) {
98                 assert(video_frame.size >= unsigned(width * height * 4));
99                 assert(!video_frame.interleaved);
100                 memcpy(video_frame.data, buffer, width * height * 4);
101                 video_frame.len = video_format.stride * height;
102                 video_frame.received_timestamp = timestamp;
103         }
104         frame_callback(timecode++,
105                 video_frame, 0, video_format,
106                 FrameAllocator::Frame(), 0, AudioFormat());
107 }
108
109 #define FRAME_SIZE (8 << 20)  // 8 MB.
110
111 void CEFCapture::configure_card()
112 {
113         if (video_frame_allocator == nullptr) {
114                 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
115                 set_video_frame_allocator(owned_video_frame_allocator.get());
116         }
117 }
118
119 void CEFCapture::start_bm_capture()
120 {
121         cef_app->initialize_cef();
122
123         CefPostTask(TID_UI, new CEFTaskAdapter([this]{
124                 lock_guard<mutex> lock(browser_mutex);
125
126                 CefBrowserSettings browser_settings;
127                 browser_settings.web_security = cef_state_t::STATE_DISABLED;
128                 browser_settings.webgl = cef_state_t::STATE_ENABLED;
129                 browser_settings.windowless_frame_rate = max_fps;
130
131                 CefWindowInfo window_info;
132                 window_info.SetAsWindowless(0);
133                 browser = CefBrowserHost::CreateBrowserSync(window_info, cef_client, start_url, browser_settings, nullptr);
134                 for (function<void()> &task : deferred_tasks) {
135                         task();
136                 }
137                 deferred_tasks.clear();
138         }));
139 }
140
141 void CEFCapture::stop_dequeue_thread()
142 {
143         lock_guard<mutex> lock(browser_mutex);
144         cef_app->close_browser(browser);
145         browser = nullptr;  // Or unref_cef() will be sad.
146         cef_app->unref_cef();
147 }
148
149 std::map<uint32_t, VideoMode> CEFCapture::get_available_video_modes() const
150 {
151         VideoMode mode;
152
153         char buf[256];
154         snprintf(buf, sizeof(buf), "%ux%u", width, height);
155         mode.name = buf;
156
157         mode.autodetect = false;
158         mode.width = width;
159         mode.height = height;
160         mode.frame_rate_num = max_fps;
161         mode.frame_rate_den = 1;
162         mode.interlaced = false;
163
164         return {{ 0, mode }};
165 }
166
167 std::map<uint32_t, std::string> CEFCapture::get_available_video_inputs() const
168 {
169         return {{ 0, "HTML video input" }};
170 }
171
172 std::map<uint32_t, std::string> CEFCapture::get_available_audio_inputs() const
173 {
174         return {{ 0, "Fake HTML audio input (silence)" }};
175 }
176
177 void CEFCapture::set_video_mode(uint32_t video_mode_id)
178 {
179         assert(video_mode_id == 0);
180 }
181
182 void CEFCapture::set_video_input(uint32_t video_input_id)
183 {
184         assert(video_input_id == 0);
185 }
186
187 void CEFCapture::set_audio_input(uint32_t audio_input_id)
188 {
189         assert(audio_input_id == 0);
190 }
191
192 void NageruCEFClient::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
193 {
194         parent->OnPaint(buffer, width, height);
195 }
196
197 bool NageruCEFClient::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
198 {
199         rect = CefRect(0, 0, width, height);
200         return true;
201 }