]> git.sesse.net Git - nageru/blob - cef_capture.h
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / cef_capture.h
1 #ifndef _CEF_CAPTURE_H
2 #define _CEF_CAPTURE_H 1
3
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.
8
9 #include <assert.h>
10 #include <stdint.h>
11
12 #include <condition_variable>
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <mutex>
17 #include <set>
18 #include <string>
19 #include <thread>
20 #include <vector>
21
22 #undef CHECK
23 #include <cef_client.h>
24 #include <cef_base.h>
25 #include <cef_render_handler.h>
26
27 #include <bmusb/bmusb.h>
28
29 class CefBrowser;
30 class CefRect;
31 class CEFCapture;
32
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
36 {
37 public:
38         NageruCEFClient(int width, int height, CEFCapture *parent)
39                 : width(width), height(height), parent(parent) {}
40
41         CefRefPtr<CefRenderHandler> GetRenderHandler() override
42         {
43                 return this;
44         }
45
46         CefRefPtr<CefLoadHandler> GetLoadHandler() override
47         {
48                 return this;
49         }
50
51         // CefRenderHandler.
52
53         void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override;
54
55         bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect);
56
57         // CefLoadHandler.
58
59         void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
60
61 private:
62         int width, height;
63         CEFCapture *parent;
64
65         IMPLEMENT_REFCOUNTING(NageruCEFClient);
66 };
67
68 class CEFCapture : public bmusb::CaptureInterface
69 {
70 public:
71         CEFCapture(const std::string &url, unsigned width, unsigned height);
72         ~CEFCapture();
73
74         void set_card_index(int card_index)
75         {
76                 this->card_index = card_index;
77         }
78
79         int get_card_index() const
80         {
81                 return card_index;
82         }
83
84         void set_url(const std::string &url);
85         void reload();
86         void set_max_fps(int max_fps);
87         void execute_javascript_async(const std::string &js);
88
89         void OnPaint(const void *buffer, int width, int height);
90
91         void OnLoadEnd();
92
93         // CaptureInterface.
94         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
95         {
96                 video_frame_allocator = allocator;
97                 if (owned_video_frame_allocator.get() != allocator) {
98                         owned_video_frame_allocator.reset();
99                 }
100         }
101
102         bmusb::FrameAllocator *get_video_frame_allocator() override
103         {
104                 return video_frame_allocator;
105         }
106
107         // Does not take ownership.
108         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
109         {
110         }
111
112         bmusb::FrameAllocator *get_audio_frame_allocator() override
113         {
114                 return nullptr;
115         }
116
117         void set_frame_callback(bmusb::frame_callback_t callback) override
118         {
119                 frame_callback = callback;
120         }
121
122         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
123         {
124                 dequeue_init_callback = init;
125                 dequeue_cleanup_callback = cleanup;
126                 has_dequeue_callbacks = true;
127         }
128
129         std::string get_description() const override
130         {
131                 return description;
132         }
133
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; }
138
139         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
140         {
141                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
142         }
143
144         void set_pixel_format(bmusb::PixelFormat pixel_format) override
145         {
146                 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
147         }
148
149         bmusb::PixelFormat get_current_pixel_format() const
150         {
151                 return bmusb::PixelFormat_8BitBGRA;
152         }
153
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; }
157
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; }
161
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; }
165
166 private:
167         void post_to_cef_ui_thread(std::function<void()> &&func);
168
169         CefRefPtr<NageruCEFClient> cef_client;
170         unsigned width, height;
171         int card_index = -1;
172
173         bool has_dequeue_callbacks = false;
174         std::function<void()> dequeue_init_callback = nullptr;
175         std::function<void()> dequeue_cleanup_callback = nullptr;
176
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;
180
181         std::string description, start_url;
182         std::atomic<int> max_fps{60};
183
184         std::mutex browser_mutex;
185         CefRefPtr<CefBrowser> browser;  // Under <browser_mutex>.
186
187         // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
188         std::vector<std::function<void()>> deferred_tasks;
189
190         // Whether the last set_url() (includes the implicit one in the constructor)
191         // has loaded yet. Accessed from the CEF thread only.
192         bool loaded = false;
193
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;
197
198         int timecode = 0;
199 };
200
201 #endif  // !defined(_CEF_CAPTURE_H)