2 #define _CEF_CAPTURE_H 1
4 // CEFCapture represents a single CEF virtual capture card (usually, there would only
5 // be one globally), similar to FFmpegCapture. It owns a CefBrowser, which calls
6 // OnPaint() back every time it has a frame. Note that it runs asynchronously;
7 // there's no way to get frame-perfect sync.
12 #include <condition_variable>
23 #include <cef_client.h>
25 #include <cef_render_handler.h>
27 #include <bmusb/bmusb.h>
33 // A helper class for CEFCapture to proxy information to CEF, without becoming
34 // CEF-refcounted itself.
35 class NageruCEFClient : public CefClient, public CefRenderHandler, public CefLoadHandler
38 NageruCEFClient(int width, int height, CEFCapture *parent)
39 : width(width), height(height), parent(parent) {}
41 CefRefPtr<CefRenderHandler> GetRenderHandler() override
46 CefRefPtr<CefLoadHandler> GetLoadHandler() override
53 void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override;
55 bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect);
59 void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
65 IMPLEMENT_REFCOUNTING(NageruCEFClient);
68 class CEFCapture : public bmusb::CaptureInterface
71 CEFCapture(const std::string &url, unsigned width, unsigned height);
74 void set_card_index(int card_index)
76 this->card_index = card_index;
79 int get_card_index() const
84 void set_url(const std::string &url);
86 void set_max_fps(int max_fps);
87 void execute_javascript_async(const std::string &js);
88 void resize(unsigned width, unsigned height);
89 void request_new_frame();
91 // Callbacks from NageruCEFClient.
92 void OnPaint(const void *buffer, int width, int height);
93 bool GetViewRect(CefRect &rect);
97 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
99 video_frame_allocator = allocator;
100 if (owned_video_frame_allocator.get() != allocator) {
101 owned_video_frame_allocator.reset();
105 bmusb::FrameAllocator *get_video_frame_allocator() override
107 return video_frame_allocator;
110 // Does not take ownership.
111 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
115 bmusb::FrameAllocator *get_audio_frame_allocator() override
120 void set_frame_callback(bmusb::frame_callback_t callback) override
122 frame_callback = callback;
125 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
127 dequeue_init_callback = init;
128 dequeue_cleanup_callback = cleanup;
129 has_dequeue_callbacks = true;
132 std::string get_description() const override
137 void configure_card() override;
138 void start_bm_capture() override;
139 void stop_dequeue_thread() override;
140 bool get_disconnected() const override { return false; }
142 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
144 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
147 void set_pixel_format(bmusb::PixelFormat pixel_format) override
149 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
152 bmusb::PixelFormat get_current_pixel_format() const
154 return bmusb::PixelFormat_8BitBGRA;
157 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
158 void set_video_mode(uint32_t video_mode_id) override;
159 uint32_t get_current_video_mode() const override { return 0; }
161 std::map<uint32_t, std::string> get_available_video_inputs() const override;
162 void set_video_input(uint32_t video_input_id) override;
163 uint32_t get_current_video_input() const override { return 0; }
165 std::map<uint32_t, std::string> get_available_audio_inputs() const override;
166 void set_audio_input(uint32_t audio_input_id) override;
167 uint32_t get_current_audio_input() const override { return 0; }
170 void post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms = 0);
172 CefRefPtr<NageruCEFClient> cef_client;
174 // Needs to be different from browser_mutex below, since GetViewRect
175 // can be called unpredictably from when we are already holding
177 std::mutex resolution_mutex;
178 unsigned width, height; // Under <resolution_mutex>.
182 bool has_dequeue_callbacks = false;
183 std::function<void()> dequeue_init_callback = nullptr;
184 std::function<void()> dequeue_cleanup_callback = nullptr;
186 bmusb::FrameAllocator *video_frame_allocator = nullptr;
187 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
188 bmusb::frame_callback_t frame_callback = nullptr;
190 std::string description, start_url;
191 std::atomic<int> max_fps{60};
193 // Needs to be recursive because the lambda in OnPaint could cause
194 // OnPaint itself to be called.
195 std::recursive_mutex browser_mutex;
196 CefRefPtr<CefBrowser> browser; // Under <browser_mutex>.
198 // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
199 std::vector<std::function<void()>> deferred_tasks;
201 // Whether the last set_url() (includes the implicit one in the constructor)
202 // has loaded yet. Accessed from the CEF thread only.
205 // JavaScript waiting for the first page (well, any page) to have loaded.
206 // Accessed from the CEF thread only.
207 std::vector<std::string> deferred_javascript;
212 #endif // !defined(_CEF_CAPTURE_H)