]> git.sesse.net Git - nageru/blob - theme.h
Implement basic support for CEF.
[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         // Should be called as part of HTMLInput.new() only.
73         void register_html_input(CEFCapture *capture)
74         {
75                 html_inputs.push_back(capture);
76         }
77
78         std::vector<CEFCapture *> get_html_inputs() const
79         {
80                 return html_inputs;
81         }
82
83         void register_video_signal_connection(LiveInputWrapper *live_input, FFmpegCapture *capture)
84         {
85                 video_signal_connections.emplace_back(live_input, capture);
86         }
87
88         std::vector<std::pair<LiveInputWrapper *, FFmpegCapture *>> get_video_signal_connections() const
89         {
90                 return video_signal_connections;
91         }
92
93         void register_html_signal_connection(LiveInputWrapper *live_input, CEFCapture *capture)
94         {
95                 html_signal_connections.emplace_back(live_input, capture);
96         }
97
98         std::vector<std::pair<LiveInputWrapper *, CEFCapture *>> get_html_signal_connections() const
99         {
100                 return html_signal_connections;
101         }
102
103 private:
104         void register_constants();
105         void register_class(const char *class_name, const luaL_Reg *funcs);
106
107         std::mutex m;
108         lua_State *L;  // Protected by <m>.
109         const InputState *input_state = nullptr;  // Protected by <m>. Only set temporarily, during chain setup.
110         movit::ResourcePool *resource_pool;
111         int num_channels;
112         unsigned num_cards;
113
114         std::mutex map_m;
115         std::map<int, int> signal_to_card_mapping;  // Protected by <map_m>.
116
117         std::vector<FFmpegCapture *> video_inputs;
118         std::vector<std::pair<LiveInputWrapper *, FFmpegCapture *>> video_signal_connections;
119         std::vector<CEFCapture *> html_inputs;
120         std::vector<std::pair<LiveInputWrapper *, CEFCapture *>> html_signal_connections;
121
122         friend class LiveInputWrapper;
123 };
124
125 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
126 // the Lua code. It contains a function (connect_signal()) intended
127 // to be called during chain setup, that picks out the current frame
128 // (in the form of a set of textures) from the input state given by
129 // the mixer, and communicates that state over to the actual YCbCrInput.
130 class LiveInputWrapper {
131 public:
132         // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
133         LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace);
134
135         void connect_signal(int signal_num);  // Must be called with the theme's <m> lock held, since it accesses theme->input_state.
136         void connect_signal_raw(int signal_num, const InputState &input_state);
137         movit::Effect *get_effect() const
138         {
139                 if (deinterlace) {
140                         return deinterlace_effect;
141                 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
142                         return rgba_inputs[0];
143                 } else {
144                         return ycbcr_inputs[0];
145                 }
146         }
147
148 private:
149         Theme *theme;  // Not owned by us.
150         bmusb::PixelFormat pixel_format;
151         movit::YCbCrFormat input_ycbcr_format;
152         std::vector<movit::YCbCrInput *> ycbcr_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
153         std::vector<movit::FlatInput *> rgba_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
154         movit::Effect *deinterlace_effect = nullptr;  // Owned by the chain.
155         bool deinterlace;
156 };
157
158 #endif  // !defined(_THEME_H)