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);
89 void OnPaint(const void *buffer, int width, int height);
94 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
96 video_frame_allocator = allocator;
97 if (owned_video_frame_allocator.get() != allocator) {
98 owned_video_frame_allocator.reset();
102 bmusb::FrameAllocator *get_video_frame_allocator() override
104 return video_frame_allocator;
107 // Does not take ownership.
108 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
112 bmusb::FrameAllocator *get_audio_frame_allocator() override
117 void set_frame_callback(bmusb::frame_callback_t callback) override
119 frame_callback = callback;
122 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
124 dequeue_init_callback = init;
125 dequeue_cleanup_callback = cleanup;
126 has_dequeue_callbacks = true;
129 std::string get_description() const override
134 void configure_card() override;
135 void start_bm_capture() override;
136 void stop_dequeue_thread() override;
137 bool get_disconnected() const override { return false; }
139 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
141 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
144 void set_pixel_format(bmusb::PixelFormat pixel_format) override
146 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
149 bmusb::PixelFormat get_current_pixel_format() const
151 return bmusb::PixelFormat_8BitBGRA;
154 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
155 void set_video_mode(uint32_t video_mode_id) override;
156 uint32_t get_current_video_mode() const override { return 0; }
158 std::map<uint32_t, std::string> get_available_video_inputs() const override;
159 void set_video_input(uint32_t video_input_id) override;
160 uint32_t get_current_video_input() const override { return 0; }
162 std::map<uint32_t, std::string> get_available_audio_inputs() const override;
163 void set_audio_input(uint32_t audio_input_id) override;
164 uint32_t get_current_audio_input() const override { return 0; }
167 void post_to_cef_ui_thread(std::function<void()> &&func);
169 CefRefPtr<NageruCEFClient> cef_client;
170 unsigned width, height;
173 bool has_dequeue_callbacks = false;
174 std::function<void()> dequeue_init_callback = nullptr;
175 std::function<void()> dequeue_cleanup_callback = nullptr;
177 bmusb::FrameAllocator *video_frame_allocator = nullptr;
178 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
179 bmusb::frame_callback_t frame_callback = nullptr;
181 std::string description, start_url;
182 std::atomic<int> max_fps{60};
184 std::mutex browser_mutex;
185 CefRefPtr<CefBrowser> browser; // Under <browser_mutex>.
187 // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
188 std::vector<std::function<void()>> deferred_tasks;
190 // Whether the last set_url() (includes the implicit one in the constructor)
191 // has loaded yet. Accessed from the CEF thread only.
194 // JavaScript waiting for the first page (well, any page) to have loaded.
195 // Accessed from the CEF thread only.
196 std::vector<std::string> deferred_javascript;
201 #endif // !defined(_CEF_CAPTURE_H)