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