]> git.sesse.net Git - nageru/blob - cef_capture.h
Implement HTMLInput::set_max_fps().
[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
21 #undef CHECK
22 #include <cef_client.h>
23 #include <cef_base.h>
24 #include <cef_render_handler.h>
25
26 #include <bmusb/bmusb.h>
27
28 class CefBrowser;
29 class CefRect;
30 class CEFCapture;
31
32 // A helper class for CEFCapture to proxy information to CEF, without becoming
33 // CEF-refcounted itself.
34 class NageruCEFClient : public CefClient, public CefRenderHandler
35 {
36 public:
37         NageruCEFClient(int width, int height, CEFCapture *parent)
38                 : width(width), height(height), parent(parent) {}
39
40         CefRefPtr<CefRenderHandler> GetRenderHandler() override
41         {
42                 return this;
43         }
44
45         void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override;
46
47         bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect);
48
49 private:
50         int width, height;
51         CEFCapture *parent;
52
53         IMPLEMENT_REFCOUNTING(NageruCEFClient);
54 };
55
56 class CEFCapture : public bmusb::CaptureInterface
57 {
58 public:
59         CEFCapture(const std::string &url, unsigned width, unsigned height);
60         ~CEFCapture();
61
62         void set_card_index(int card_index)
63         {
64                 this->card_index = card_index;
65         }
66
67         int get_card_index() const
68         {
69                 return card_index;
70         }
71
72         void set_url(const std::string &url);
73         void reload();
74         void set_max_fps(int max_fps);
75
76         void OnPaint(const void *buffer, int width, int height);
77
78         // CaptureInterface.
79         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
80         {
81                 video_frame_allocator = allocator;
82                 if (owned_video_frame_allocator.get() != allocator) {
83                         owned_video_frame_allocator.reset();
84                 }
85         }
86
87         bmusb::FrameAllocator *get_video_frame_allocator() override
88         {
89                 return video_frame_allocator;
90         }
91
92         // Does not take ownership.
93         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
94         {
95         }
96
97         bmusb::FrameAllocator *get_audio_frame_allocator() override
98         {
99                 return nullptr;
100         }
101
102         void set_frame_callback(bmusb::frame_callback_t callback) override
103         {
104                 frame_callback = callback;
105         }
106
107         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
108         {
109                 dequeue_init_callback = init;
110                 dequeue_cleanup_callback = cleanup;
111                 has_dequeue_callbacks = true;
112         }
113
114         std::string get_description() const override
115         {
116                 return description;
117         }
118
119         void configure_card() override;
120         void start_bm_capture() override;
121         void stop_dequeue_thread() override;
122         bool get_disconnected() const override { return false; }
123
124         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override
125         {
126                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
127         }
128
129         void set_pixel_format(bmusb::PixelFormat pixel_format) override
130         {
131                 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
132         }
133
134         bmusb::PixelFormat get_current_pixel_format() const
135         {
136                 return bmusb::PixelFormat_8BitBGRA;
137         }
138
139         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
140         void set_video_mode(uint32_t video_mode_id) override;
141         uint32_t get_current_video_mode() const override { return 0; }
142
143         std::map<uint32_t, std::string> get_available_video_inputs() const override;
144         void set_video_input(uint32_t video_input_id) override;
145         uint32_t get_current_video_input() const override { return 0; }
146
147         std::map<uint32_t, std::string> get_available_audio_inputs() const override;
148         void set_audio_input(uint32_t audio_input_id) override;
149         uint32_t get_current_audio_input() const override { return 0; }
150
151 private:
152         void post_to_cef_ui_thread(std::function<void()> &&func);
153
154         CefRefPtr<NageruCEFClient> cef_client;
155         unsigned width, height;
156         int card_index = -1;
157
158         bool has_dequeue_callbacks = false;
159         std::function<void()> dequeue_init_callback = nullptr;
160         std::function<void()> dequeue_cleanup_callback = nullptr;
161
162         bmusb::FrameAllocator *video_frame_allocator = nullptr;
163         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
164         bmusb::frame_callback_t frame_callback = nullptr;
165
166         std::string description, start_url;
167         std::atomic<int> max_fps{60};
168
169         std::mutex browser_mutex;
170         CefRefPtr<CefBrowser> browser;  // Under <browser_mutex>.
171
172         // Tasks waiting for <browser> to get ready. Under <browser_mutex>.
173         std::vector<std::function<void()>> deferred_tasks;
174
175         int timecode = 0;
176 };
177
178 #endif  // !defined(_CEF_CAPTURE_H)