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(CEFCapture *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) override;
59 void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
64 IMPLEMENT_REFCOUNTING(NageruCEFClient);
67 class CEFCapture : public bmusb::CaptureInterface
70 CEFCapture(const std::string &url, unsigned width, unsigned height);
73 void set_card_index(int card_index)
75 this->card_index = card_index;
78 int get_card_index() const
83 void set_url(const std::string &url);
85 void set_max_fps(int max_fps);
86 void execute_javascript_async(const std::string &js);
87 void resize(unsigned width, unsigned height);
88 void request_new_frame();
90 // Callbacks from NageruCEFClient.
91 void OnPaint(const void *buffer, int width, int height);
92 bool GetViewRect(CefRect &rect);
96 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
98 video_frame_allocator = allocator;
99 if (owned_video_frame_allocator.get() != allocator) {
100 owned_video_frame_allocator.reset();
104 bmusb::FrameAllocator *get_video_frame_allocator() override
106 return video_frame_allocator;
109 // Does not take ownership.
110 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
114 bmusb::FrameAllocator *get_audio_frame_allocator() override
119 void set_frame_callback(bmusb::frame_callback_t callback) override
121 frame_callback = callback;
124 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
126 dequeue_init_callback = init;
127 dequeue_cleanup_callback = cleanup;
128 has_dequeue_callbacks = true;
131 std::string get_description() const override
136 void configure_card() override;
137 void start_bm_capture() override;
138 void stop_dequeue_thread() override;
139 bool get_disconnected() const override { return false; }
141 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
143 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
146 void set_pixel_format(bmusb::PixelFormat pixel_format) override
148 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
151 bmusb::PixelFormat get_current_pixel_format() const override
153 return bmusb::PixelFormat_8BitBGRA;
156 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
157 void set_video_mode(uint32_t video_mode_id) override;
158 uint32_t get_current_video_mode() const override { return 0; }
160 std::map<uint32_t, std::string> get_available_video_inputs() const override;
161 void set_video_input(uint32_t video_input_id) override;
162 uint32_t get_current_video_input() const override { return 0; }
164 std::map<uint32_t, std::string> get_available_audio_inputs() const override;
165 void set_audio_input(uint32_t audio_input_id) override;
166 uint32_t get_current_audio_input() const override { return 0; }
169 void post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms = 0);
171 CefRefPtr<NageruCEFClient> cef_client;
173 // Needs to be different from browser_mutex below, since GetViewRect
174 // can be called unpredictably from when we are already holding
176 std::mutex resolution_mutex;
177 unsigned width, height; // Under <resolution_mutex>.
181 bool has_dequeue_callbacks = false;
182 std::function<void()> dequeue_init_callback = nullptr;
183 std::function<void()> dequeue_cleanup_callback = nullptr;
185 bmusb::FrameAllocator *video_frame_allocator = nullptr;
186 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
187 bmusb::frame_callback_t frame_callback = nullptr;
189 std::string description, start_url;
190 std::atomic<int> max_fps{60};
192 // Needs to be recursive because the lambda in OnPaint could cause
193 // OnPaint itself to be called.
194 std::recursive_mutex browser_mutex;
195 CefRefPtr<CefBrowser> browser; // Under <browser_mutex>.
197 // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
198 std::vector<std::function<void()>> deferred_tasks;
200 // Whether the last set_url() (includes the implicit one in the constructor)
201 // has loaded yet. Accessed from the CEF thread only.
204 // JavaScript waiting for the first page (well, any page) to have loaded.
205 // Accessed from the CEF thread only.
206 std::vector<std::string> deferred_javascript;
211 #endif // !defined(_CEF_CAPTURE_H)