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>
22 #include <cef_client.h>
24 #include <cef_render_handler.h>
26 #include <bmusb/bmusb.h>
32 // A helper class for CEFCapture to proxy information to CEF, without becoming
33 // CEF-refcounted itself.
34 class NageruCEFClient : public CefClient, public CefRenderHandler
37 NageruCEFClient(int width, int height, CEFCapture *parent)
38 : width(width), height(height), parent(parent) {}
40 CefRefPtr<CefRenderHandler> GetRenderHandler() override
45 void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override;
47 bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect);
53 IMPLEMENT_REFCOUNTING(NageruCEFClient);
56 class CEFCapture : public bmusb::CaptureInterface
59 CEFCapture(const std::string &url, unsigned width, unsigned height);
62 void set_card_index(int card_index)
64 this->card_index = card_index;
67 int get_card_index() const
72 void set_url(const std::string &url);
74 void set_max_fps(int max_fps);
75 void execute_javascript_async(const std::string &js);
77 void OnPaint(const void *buffer, int width, int height);
80 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
82 video_frame_allocator = allocator;
83 if (owned_video_frame_allocator.get() != allocator) {
84 owned_video_frame_allocator.reset();
88 bmusb::FrameAllocator *get_video_frame_allocator() override
90 return video_frame_allocator;
93 // Does not take ownership.
94 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
98 bmusb::FrameAllocator *get_audio_frame_allocator() override
103 void set_frame_callback(bmusb::frame_callback_t callback) override
105 frame_callback = callback;
108 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
110 dequeue_init_callback = init;
111 dequeue_cleanup_callback = cleanup;
112 has_dequeue_callbacks = true;
115 std::string get_description() const override
120 void configure_card() override;
121 void start_bm_capture() override;
122 void stop_dequeue_thread() override;
123 bool get_disconnected() const override { return false; }
125 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
127 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
130 void set_pixel_format(bmusb::PixelFormat pixel_format) override
132 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
135 bmusb::PixelFormat get_current_pixel_format() const
137 return bmusb::PixelFormat_8BitBGRA;
140 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
141 void set_video_mode(uint32_t video_mode_id) override;
142 uint32_t get_current_video_mode() const override { return 0; }
144 std::map<uint32_t, std::string> get_available_video_inputs() const override;
145 void set_video_input(uint32_t video_input_id) override;
146 uint32_t get_current_video_input() const override { return 0; }
148 std::map<uint32_t, std::string> get_available_audio_inputs() const override;
149 void set_audio_input(uint32_t audio_input_id) override;
150 uint32_t get_current_audio_input() const override { return 0; }
153 void post_to_cef_ui_thread(std::function<void()> &&func);
155 CefRefPtr<NageruCEFClient> cef_client;
156 unsigned width, height;
159 bool has_dequeue_callbacks = false;
160 std::function<void()> dequeue_init_callback = nullptr;
161 std::function<void()> dequeue_cleanup_callback = nullptr;
163 bmusb::FrameAllocator *video_frame_allocator = nullptr;
164 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
165 bmusb::frame_callback_t frame_callback = nullptr;
167 std::string description, start_url;
168 std::atomic<int> max_fps{60};
170 std::mutex browser_mutex;
171 CefRefPtr<CefBrowser> browser; // Under <browser_mutex>.
173 // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
174 std::vector<std::function<void()>> deferred_tasks;
179 #endif // !defined(_CEF_CAPTURE_H)