]> git.sesse.net Git - nageru/blob - theme.h
Release Nageru 1.7.2.
[nageru] / theme.h
1 #ifndef _THEME_H
2 #define _THEME_H 1
3
4 #include <lua.hpp>
5 #include <movit/flat_input.h>
6 #include <movit/ycbcr_input.h>
7 #include <stdbool.h>
8 #include <functional>
9 #include <map>
10 #include <mutex>
11 #include <string>
12 #include <unordered_map>
13 #include <vector>
14
15 #include "bmusb/bmusb.h"
16 #include "ref_counted_frame.h"
17 #include "tweaked_inputs.h"
18
19 class CEFCapture;
20 class FFmpegCapture;
21 class LiveInputWrapper;
22 struct InputState;
23
24 namespace movit {
25 class Effect;
26 class EffectChain;
27 class ResourcePool;
28 }  // namespace movit
29
30 class Theme {
31 public:
32         Theme(const std::string &filename, const std::vector<std::string> &search_dirs, movit::ResourcePool *resource_pool, unsigned num_cards);
33         ~Theme();
34
35         struct Chain {
36                 movit::EffectChain *chain;
37                 std::function<void()> setup_chain;
38
39                 // FRAME_HISTORY frames for each input, in order. Will contain duplicates
40                 // for non-interlaced inputs.
41                 std::vector<RefCountedFrame> input_frames;
42         };
43
44         Chain get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state);
45
46         int get_num_channels() const { return num_channels; }
47         int map_signal(int signal_num);
48         void set_signal_mapping(int signal_num, int card_num);
49         std::string get_channel_name(unsigned channel);
50         int get_channel_signal(unsigned channel);
51         bool get_supports_set_wb(unsigned channel);
52         void set_wb(unsigned channel, double r, double g, double b);
53         std::string get_channel_color(unsigned channel);
54
55         std::vector<std::string> get_transition_names(float t);
56
57         void transition_clicked(int transition_num, float t);
58         void channel_clicked(int preview_num);
59
60         movit::ResourcePool *get_resource_pool() const { return resource_pool; }
61
62         // Should be called as part of VideoInput.new() only.
63         void register_video_input(FFmpegCapture *capture)
64         {
65                 video_inputs.push_back(capture);
66         }
67
68         std::vector<FFmpegCapture *> get_video_inputs() const
69         {
70                 return video_inputs;
71         }
72
73 #ifdef HAVE_CEF
74         // Should be called as part of HTMLInput.new() only.
75         void register_html_input(CEFCapture *capture)
76         {
77                 html_inputs.push_back(capture);
78         }
79
80         std::vector<CEFCapture *> get_html_inputs() const
81         {
82                 return html_inputs;
83         }
84 #endif
85
86         void register_video_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, FFmpegCapture *capture)
87         {
88                 video_signal_connections[chain].emplace_back(VideoSignalConnection { live_input, capture });
89         }
90
91 #ifdef HAVE_CEF
92         void register_html_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, CEFCapture *capture)
93         {
94                 html_signal_connections[chain].emplace_back(CEFSignalConnection { live_input, capture });
95         }
96 #endif
97
98         struct MenuEntry {
99                 std::string text;
100                 int lua_ref;
101         };
102         std::vector<MenuEntry> get_theme_menu() { return theme_menu; }  // Can be empty for no menu.
103         void theme_menu_entry_clicked(int lua_ref);
104
105         // Will be invoked every time the theme sets a new menu.
106         // Is not invoked for a menu that exists at the time of the callback.
107         void set_theme_menu_callback(std::function<void()> callback)
108         {
109                 theme_menu_callback = callback;
110         }
111
112 private:
113         void register_constants();
114         void register_class(const char *class_name, const luaL_Reg *funcs);
115         int set_theme_menu(lua_State *L);
116
117         std::string theme_path;
118
119         std::mutex m;
120         lua_State *L;  // Protected by <m>.
121         const InputState *input_state = nullptr;  // Protected by <m>. Only set temporarily, during chain setup.
122         movit::ResourcePool *resource_pool;
123         int num_channels;
124         unsigned num_cards;
125
126         std::mutex map_m;
127         std::map<int, int> signal_to_card_mapping;  // Protected by <map_m>.
128
129         std::vector<FFmpegCapture *> video_inputs;
130         struct VideoSignalConnection {
131                 LiveInputWrapper *wrapper;
132                 FFmpegCapture *source;
133         };
134         std::unordered_map<movit::EffectChain *, std::vector<VideoSignalConnection>>
135                  video_signal_connections;
136 #ifdef HAVE_CEF
137         std::vector<CEFCapture *> html_inputs;
138         struct CEFSignalConnection {
139                 LiveInputWrapper *wrapper;
140                 CEFCapture *source;
141         };
142         std::unordered_map<movit::EffectChain *, std::vector<CEFSignalConnection>>
143                 html_signal_connections;
144 #endif
145
146         std::vector<MenuEntry> theme_menu;
147         std::function<void()> theme_menu_callback;
148
149         friend class LiveInputWrapper;
150         friend int ThemeMenu_set(lua_State *L);
151 };
152
153 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
154 // the Lua code. It contains a function (connect_signal()) intended
155 // to be called during chain setup, that picks out the current frame
156 // (in the form of a set of textures) from the input state given by
157 // the mixer, and communicates that state over to the actual YCbCrInput.
158 class LiveInputWrapper {
159 public:
160         // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
161         LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace, bool user_connectable);
162
163         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.
164         void connect_signal_raw(int signal_num, const InputState &input_state);
165         movit::Effect *get_effect() const
166         {
167                 if (deinterlace) {
168                         return deinterlace_effect;
169                 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
170                         return rgba_inputs[0];
171                 } else {
172                         return ycbcr_inputs[0];
173                 }
174         }
175
176 private:
177         Theme *theme;  // Not owned by us.
178         bmusb::PixelFormat pixel_format;
179         movit::YCbCrFormat input_ycbcr_format;
180         std::vector<movit::YCbCrInput *> ycbcr_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
181         std::vector<movit::FlatInput *> rgba_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
182         movit::Effect *deinterlace_effect = nullptr;  // Owned by the chain.
183         bool deinterlace;
184         bool user_connectable;
185 };
186
187 #endif  // !defined(_THEME_H)