]> git.sesse.net Git - nageru/blob - cef_capture.h
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[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(CEFCapture *parent)
39                 : 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) override;
56
57         // CefLoadHandler.
58
59         void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;
60
61 private:
62         CEFCapture *parent;
63
64         IMPLEMENT_REFCOUNTING(NageruCEFClient);
65 };
66
67 class CEFCapture : public bmusb::CaptureInterface
68 {
69 public:
70         CEFCapture(const std::string &url, unsigned width, unsigned height);
71         ~CEFCapture();
72
73         void set_card_index(int card_index)
74         {
75                 this->card_index = card_index;
76         }
77
78         int get_card_index() const
79         {
80                 return card_index;
81         }
82
83         void set_url(const std::string &url);
84         void reload();
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();
89
90         // Callbacks from NageruCEFClient.
91         void OnPaint(const void *buffer, int width, int height);
92         bool GetViewRect(CefRect &rect);
93         void OnLoadEnd();
94
95         // CaptureInterface.
96         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
97         {
98                 video_frame_allocator = allocator;
99                 if (owned_video_frame_allocator.get() != allocator) {
100                         owned_video_frame_allocator.reset();
101                 }
102         }
103
104         bmusb::FrameAllocator *get_video_frame_allocator() override
105         {
106                 return video_frame_allocator;
107         }
108
109         // Does not take ownership.
110         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
111         {
112         }
113
114         bmusb::FrameAllocator *get_audio_frame_allocator() override
115         {
116                 return nullptr;
117         }
118
119         void set_frame_callback(bmusb::frame_callback_t callback) override
120         {
121                 frame_callback = callback;
122         }
123
124         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
125         {
126                 dequeue_init_callback = init;
127                 dequeue_cleanup_callback = cleanup;
128                 has_dequeue_callbacks = true;
129         }
130
131         std::string get_description() const override
132         {
133                 return description;
134         }
135
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; }
140
141         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
142         {
143                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
144         }
145
146         void set_pixel_format(bmusb::PixelFormat pixel_format) override
147         {
148                 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
149         }
150
151         bmusb::PixelFormat get_current_pixel_format() const override
152         {
153                 return bmusb::PixelFormat_8BitBGRA;
154         }
155
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; }
159
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; }
163
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; }
167
168 private:
169         void post_to_cef_ui_thread(std::function<void()> &&func, int64_t delay_ms = 0);
170
171         CefRefPtr<NageruCEFClient> cef_client;
172
173         // Needs to be different from browser_mutex below, since GetViewRect
174         // can be called unpredictably from when we are already holding
175         // <browser_mutex>.
176         std::mutex resolution_mutex;
177         unsigned width, height;  // Under <resolution_mutex>.
178
179         int card_index = -1;
180
181         bool has_dequeue_callbacks = false;
182         std::function<void()> dequeue_init_callback = nullptr;
183         std::function<void()> dequeue_cleanup_callback = nullptr;
184
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;
188
189         std::string description, start_url;
190         std::atomic<int> max_fps{60};
191
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>.
196
197         // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
198         std::vector<std::function<void()>> deferred_tasks;
199
200         // Whether the last set_url() (includes the implicit one in the constructor)
201         // has loaded yet. Accessed from the CEF thread only.
202         bool loaded = false;
203
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;
207
208         int timecode = 0;
209 };
210
211 #endif  // !defined(_CEF_CAPTURE_H)