]> git.sesse.net Git - nageru/blob - theme.h
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[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 private:
108         void register_constants();
109         void register_class(const char *class_name, const luaL_Reg *funcs);
110
111         std::mutex m;
112         lua_State *L;  // Protected by <m>.
113         const InputState *input_state = nullptr;  // Protected by <m>. Only set temporarily, during chain setup.
114         movit::ResourcePool *resource_pool;
115         int num_channels;
116         unsigned num_cards;
117
118         std::mutex map_m;
119         std::map<int, int> signal_to_card_mapping;  // Protected by <map_m>.
120
121         std::vector<FFmpegCapture *> video_inputs;
122         std::vector<std::pair<LiveInputWrapper *, FFmpegCapture *>> video_signal_connections;
123 #ifdef HAVE_CEF
124         std::vector<CEFCapture *> html_inputs;
125         std::vector<std::pair<LiveInputWrapper *, CEFCapture *>> html_signal_connections;
126 #endif
127
128         friend class LiveInputWrapper;
129 };
130
131 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
132 // the Lua code. It contains a function (connect_signal()) intended
133 // to be called during chain setup, that picks out the current frame
134 // (in the form of a set of textures) from the input state given by
135 // the mixer, and communicates that state over to the actual YCbCrInput.
136 class LiveInputWrapper {
137 public:
138         // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
139         LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace);
140
141         void connect_signal(int signal_num);  // Must be called with the theme's <m> lock held, since it accesses theme->input_state.
142         void connect_signal_raw(int signal_num, const InputState &input_state);
143         movit::Effect *get_effect() const
144         {
145                 if (deinterlace) {
146                         return deinterlace_effect;
147                 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
148                         return rgba_inputs[0];
149                 } else {
150                         return ycbcr_inputs[0];
151                 }
152         }
153
154 private:
155         Theme *theme;  // Not owned by us.
156         bmusb::PixelFormat pixel_format;
157         movit::YCbCrFormat input_ycbcr_format;
158         std::vector<movit::YCbCrInput *> ycbcr_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
159         std::vector<movit::FlatInput *> rgba_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
160         movit::Effect *deinterlace_effect = nullptr;  // Owned by the chain.
161         bool deinterlace;
162 };
163
164 #endif  // !defined(_THEME_H)