]> git.sesse.net Git - nageru/blob - cef_capture.h
Fix some compiler warnings.
[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         void resize(unsigned width, unsigned height);
89         void request_new_frame();
90
91         // Callbacks from NageruCEFClient.
92         void OnPaint(const void *buffer, int width, int height);
93         bool GetViewRect(CefRect &rect);
94         void OnLoadEnd();
95
96         // CaptureInterface.
97         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
98         {
99                 video_frame_allocator = allocator;
100                 if (owned_video_frame_allocator.get() != allocator) {
101                         owned_video_frame_allocator.reset();
102                 }
103         }
104
105         bmusb::FrameAllocator *get_video_frame_allocator() override
106         {
107                 return video_frame_allocator;
108         }
109
110         // Does not take ownership.
111         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
112         {
113         }
114
115         bmusb::FrameAllocator *get_audio_frame_allocator() override
116         {
117                 return nullptr;
118         }
119
120         void set_frame_callback(bmusb::frame_callback_t callback) override
121         {
122                 frame_callback = callback;
123         }
124
125         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
126         {
127                 dequeue_init_callback = init;
128                 dequeue_cleanup_callback = cleanup;
129                 has_dequeue_callbacks = true;
130         }
131
132         std::string get_description() const override
133         {
134                 return description;
135         }
136
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; }
141
142         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
143         {
144                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
145         }
146
147         void set_pixel_format(bmusb::PixelFormat pixel_format) override
148         {
149                 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
150         }
151
152         bmusb::PixelFormat get_current_pixel_format() const
153         {
154                 return bmusb::PixelFormat_8BitBGRA;
155         }
156
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; }
160
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; }
164
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; }
168
169 private:
170         void post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms = 0);
171
172         CefRefPtr<NageruCEFClient> cef_client;
173
174         // Needs to be different from browser_mutex below, since GetViewRect
175         // can be called unpredictably from when we are already holding
176         // <browser_mutex>.
177         std::mutex resolution_mutex;
178         unsigned width, height;  // Under <resolution_mutex>.
179
180         int card_index = -1;
181
182         bool has_dequeue_callbacks = false;
183         std::function<void()> dequeue_init_callback = nullptr;
184         std::function<void()> dequeue_cleanup_callback = nullptr;
185
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;
189
190         std::string description, start_url;
191         std::atomic<int> max_fps{60};
192
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>.
197
198         // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
199         std::vector<std::function<void()>> deferred_tasks;
200
201         // Whether the last set_url() (includes the implicit one in the constructor)
202         // has loaded yet. Accessed from the CEF thread only.
203         bool loaded = false;
204
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;
208
209         int timecode = 0;
210 };
211
212 #endif  // !defined(_CEF_CAPTURE_H)