]> git.sesse.net Git - nageru/blob - nageru/theme.h
Set CEF autoplay policy to be more lenient.
[nageru] / nageru / theme.h
1 #ifndef _THEME_H
2 #define _THEME_H 1
3
4 #include <lua.hpp>
5 #include <movit/effect.h>
6 #include <movit/flat_input.h>
7 #include <movit/ycbcr_input.h>
8 #include <array>
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <string>
14 #include <unordered_map>
15 #include <utility>
16 #include <vector>
17
18 #include "bmusb/bmusb.h"
19 #include "ref_counted_frame.h"
20 #include "shared/shared_defs.h"
21 #include "tweaked_inputs.h"
22
23 class Scene;
24 class CEFCapture;
25 class FFmpegCapture;
26 class LiveInputWrapper;
27 struct InputState;
28
29 namespace movit {
30 class Effect;
31 class EffectChain;
32 class ResourcePool;
33 }  // namespace movit
34
35 enum EffectType {
36         // LIVE_INPUT_* also covers CEF and video inputs.
37         LIVE_INPUT_YCBCR,
38         LIVE_INPUT_YCBCR_WITH_DEINTERLACE,
39         LIVE_INPUT_YCBCR_PLANAR,
40         LIVE_INPUT_BGRA,
41         IMAGE_INPUT,
42
43         IDENTITY_EFFECT,
44         WHITE_BALANCE_EFFECT,
45         AUTO_WHITE_BALANCE_EFFECT,  // Same as WHITE_BALANCE_EFFECT, but sets its value automatically.
46         RESAMPLE_EFFECT,
47         PADDING_EFFECT,
48         INTEGRAL_PADDING_EFFECT,
49         OVERLAY_EFFECT,
50         RESIZE_EFFECT,
51         MULTIPLY_EFFECT,
52         MIX_EFFECT,
53         LIFT_GAMMA_GAIN_EFFECT,
54         BLUR_EFFECT,
55         UNSHARP_MASK_EFFECT,
56
57         NO_EFFECT_TYPE
58 };
59
60 // An EffectBlueprint refers to an Effect before it's being added to the graph.
61 // It contains enough information to instantiate the effect, including any
62 // parameters that were set before it was added to the graph. Once it is
63 // instantiated, it forwards its calls on to the real Effect instead.
64 struct EffectBlueprint {
65         EffectBlueprint(EffectType effect_type) : effect_type(effect_type) {}
66
67         EffectType effect_type;
68         std::map<std::string, int> int_parameters;
69         std::map<std::string, float> float_parameters;
70         std::map<std::string, std::array<float, 3>> vec3_parameters;
71         std::map<std::string, std::array<float, 4>> vec4_parameters;
72
73         movit::Effect *effect = nullptr;  // Gets filled out when it's instantiated.
74 };
75
76 // Contains basically the same data as InputState, but does not hold on to
77 // a reference to the frames. This is important so that we can release them
78 // without having to wait for Lua's GC.
79 struct InputStateInfo {
80         explicit InputStateInfo(const InputState& input_state);
81
82         unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS];
83         bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS];
84         unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS];
85         bmusb::PixelFormat last_pixel_format[MAX_VIDEO_CARDS];
86         bool has_last_subtitle[MAX_VIDEO_CARDS];
87         std::string last_subtitle[MAX_VIDEO_CARDS];
88 };
89
90 class Theme {
91 public:
92         Theme(const std::string &filename, const std::vector<std::string> &search_dirs, movit::ResourcePool *resource_pool);
93         ~Theme();
94
95         struct Chain {
96                 movit::EffectChain *chain;
97                 std::function<void()> setup_chain;
98
99                 // FRAME_HISTORY frames for each input, in order. Will contain duplicates
100                 // for non-interlaced inputs.
101                 std::vector<RefCountedFrame> input_frames;
102         };
103
104         Chain get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state);
105
106         int get_num_channels() const { return num_channels; }
107         int map_signal_to_card(int signal_num);
108         void set_signal_mapping(int signal_num, int card_idx);
109         std::string get_channel_name(unsigned channel);
110         int map_channel_to_signal(unsigned channel);
111         bool get_supports_set_wb(unsigned channel);
112         void set_wb(unsigned channel, float r, float g, float b);
113         void set_wb_for_card(int card_idx, float r, float g, float b);
114         movit::RGBTriplet get_white_balance_for_card(int card_idx);
115         std::string get_channel_color(unsigned channel);
116
117         std::unordered_map<int, movit::RGBTriplet> white_balance_for_card;
118
119         std::vector<std::string> get_transition_names(float t);
120
121         void transition_clicked(int transition_num, float t);
122         void channel_clicked(int preview_num);
123
124         movit::ResourcePool *get_resource_pool() const { return resource_pool; }
125
126         // Should be called as part of VideoInput.new() only.
127         void register_video_input(FFmpegCapture *capture)
128         {
129                 video_inputs.push_back(capture);
130         }
131
132         std::vector<FFmpegCapture *> get_video_inputs() const
133         {
134                 return video_inputs;
135         }
136
137 #ifdef HAVE_CEF
138         // Should be called as part of HTMLInput.new() only.
139         void register_html_input(CEFCapture *capture)
140         {
141                 html_inputs.push_back(capture);
142         }
143
144         std::vector<CEFCapture *> get_html_inputs() const
145         {
146                 return html_inputs;
147         }
148 #endif
149
150         void register_video_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, FFmpegCapture *capture)
151         {
152                 video_signal_connections[chain].emplace_back(VideoSignalConnection { live_input, capture });
153         }
154
155 #ifdef HAVE_CEF
156         void register_html_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, CEFCapture *capture)
157         {
158                 html_signal_connections[chain].emplace_back(CEFSignalConnection { live_input, capture });
159         }
160 #endif
161
162         struct MenuEntry {
163                 MenuEntry(const std::string &text, lua_State *L, int lua_ref, unsigned flags)
164                         : text(text), is_submenu(false), entry{L, lua_ref, flags} {}
165                 MenuEntry(const std::string &text, std::vector<std::unique_ptr<MenuEntry>> submenu)
166                         : text(text), is_submenu(true), submenu(std::move(submenu)) {}
167                 ~MenuEntry();
168
169                 static constexpr unsigned CHECKABLE = 1;
170                 static constexpr unsigned CHECKED = 2;
171
172                 std::string text;
173                 bool is_submenu;
174
175                 union {
176                         // is_submenu = false.
177                         struct {
178                                 lua_State *L;
179                                 int lua_ref;
180                                 unsigned flags;
181                         } entry;
182
183                         // is_submenu = true.
184                         std::vector<std::unique_ptr<MenuEntry>> submenu;
185                 };
186         };
187         MenuEntry *get_theme_menu() { return theme_menu.get(); }  // Can be empty for no menu.
188         void theme_menu_entry_clicked(int lua_ref);
189
190         // Will be invoked every time the theme sets a new menu.
191         // Is not invoked for a menu that exists at the time of the callback.
192         void set_theme_menu_callback(std::function<void()> callback)
193         {
194                 theme_menu_callback = callback;
195         }
196
197         std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds);
198
199         // Signal that the given card is going away and will not be replaced
200         // with a fake capture card, so remove all connections to it so that
201         // they don't automatically come back on the next frame.
202         void remove_card(unsigned card_index);
203
204 private:
205         void register_globals();
206         void register_class(const char *class_name, const luaL_Reg *funcs, EffectType effect_type = NO_EFFECT_TYPE);
207         int set_theme_menu(lua_State *L);
208         Chain get_chain_from_effect_chain(movit::EffectChain *effect_chain, unsigned num, const InputState &input_state);
209         void call_lua_wb_callback(unsigned channel, float r, float g, float b);
210
211         std::string theme_path;
212
213         std::mutex m;
214         lua_State *L;  // Protected by <m>.
215         const InputState *input_state = nullptr;  // Protected by <m>. Only set temporarily, during chain setup.
216         movit::ResourcePool *resource_pool;
217         int num_channels = -1;
218         bool startup_finished = false;
219
220         std::mutex map_m;
221         std::map<int, int> signal_to_card_mapping;  // Protected by <map_m>.
222
223         std::vector<FFmpegCapture *> video_inputs;
224         struct VideoSignalConnection {
225                 LiveInputWrapper *wrapper;
226                 FFmpegCapture *source;
227         };
228         std::unordered_map<movit::EffectChain *, std::vector<VideoSignalConnection>>
229                  video_signal_connections;
230 #ifdef HAVE_CEF
231         std::vector<CEFCapture *> html_inputs;
232         struct CEFSignalConnection {
233                 LiveInputWrapper *wrapper;
234                 CEFCapture *source;
235         };
236         std::unordered_map<movit::EffectChain *, std::vector<CEFSignalConnection>>
237                 html_signal_connections;
238 #endif
239
240         std::unique_ptr<MenuEntry> theme_menu;
241         std::function<void()> theme_menu_callback;
242
243         std::map<unsigned, std::string> channel_names;  // Set using Nageru.set_channel_name(). Protected by <m>.
244         std::map<unsigned, int> channel_signals;  // Set using Nageru.set_channel_signal(). Protected by <m>.
245         std::map<unsigned, bool> channel_supports_wb;  // Set using Nageru.set_supports_wb(). Protected by <m>.
246
247         friend class LiveInputWrapper;
248         friend class Scene;
249         friend int ThemeMenu_set(lua_State *L);
250         friend int Nageru_set_channel_name(lua_State *L);
251         friend int Nageru_set_num_channels(lua_State *L);
252         friend int Nageru_set_channel_signal(lua_State *L);
253         friend int Nageru_set_supports_wb(lua_State *L);
254 };
255
256 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
257 // the Lua code. It contains a function (connect_signal()) intended
258 // to be called during chain setup, that picks out the current frame
259 // (in the form of a set of textures) from the input state given by
260 // the mixer, and communicates that state over to the actual YCbCrInput.
261 class LiveInputWrapper {
262 public:
263         // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
264         LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace, bool user_connectable);
265
266         bool connect_signal(int signal_num);  // Must be called with the theme's <m> lock held, since it accesses theme->input_state. Returns false on error.
267         void connect_card(int signal_num, const InputState &input_state);
268         movit::Effect *get_effect() const
269         {
270                 if (deinterlace) {
271                         return deinterlace_effect;
272                 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
273                         return rgba_inputs[0];
274                 } else {
275                         return ycbcr_inputs[0];
276                 }
277         }
278
279 private:
280         Theme *theme;  // Not owned by us.
281         bmusb::PixelFormat pixel_format;
282         movit::YCbCrFormat input_ycbcr_format;
283         std::vector<movit::YCbCrInput *> ycbcr_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
284         std::vector<movit::FlatInput *> rgba_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
285         movit::Effect *deinterlace_effect = nullptr;  // Owned by the chain.
286         bool deinterlace;
287         bool user_connectable;
288 };
289
290 // Utility functions used by Scene.
291 void add_outputs_and_finalize(movit::EffectChain *chain, bool is_main_chain);
292 Theme *get_theme_updata(lua_State* L);
293 bool checkbool(lua_State* L, int idx);
294 std::string checkstdstring(lua_State *L, int index);
295 movit::Effect *instantiate_effect(movit::EffectChain *chain, EffectType effect_type);
296 void print_warning(lua_State* L, const char *format, ...);
297
298 #endif  // !defined(_THEME_H)