]> git.sesse.net Git - nageru/blob - mixer.h
Hook up the rest of the chains from Lua, too.
[nageru] / mixer.h
1 #ifndef _MIXER_H
2 #define _MIXER_H 1
3
4 // The actual video mixer, running in its own separate background thread.
5
6 #include <epoxy/gl.h>
7 #undef Success
8 #include <movit/effect_chain.h>
9 #include <movit/flat_input.h>
10 #include <functional>
11
12 #include "bmusb.h"
13 #include "h264encode.h"
14 #include "pbo_frame_allocator.h"
15 #include "ref_counted_frame.h"
16 #include "ref_counted_gl_sync.h"
17 #include "theme.h"
18
19 #define NUM_CARDS 2
20
21 namespace movit {
22 class YCbCrInput;
23 }
24 class QOpenGLContext;
25 class QSurfaceFormat;
26
27 class Mixer {
28 public:
29         // The surface format is used for offscreen destinations for OpenGL contexts we need.
30         Mixer(const QSurfaceFormat &format);
31         ~Mixer();
32         void start();
33         void quit();
34
35         enum Source {
36                 SOURCE_INPUT1,
37                 SOURCE_INPUT2,
38                 SOURCE_SBS,
39         };
40         void cut(Source source);
41
42         enum Output {
43                 OUTPUT_LIVE = 0,
44                 OUTPUT_PREVIEW,
45                 OUTPUT_INPUT0,
46                 OUTPUT_INPUT1,
47                 NUM_OUTPUTS
48         };
49
50         struct DisplayFrame {
51                 // The chain for rendering this frame. To render a display frame,
52                 // first wait for <ready_fence>, then call <setup_chain>
53                 // to wire up all the inputs, and then finally call
54                 // chain->render_to_screen() or similar.
55                 movit::EffectChain *chain;
56                 std::function<void()> setup_chain;
57
58                 // Asserted when all the inputs are ready; you cannot render the chain
59                 // before this.
60                 RefCountedGLsync ready_fence;
61
62                 // Holds on to all the input frames needed for this display frame,
63                 // so they are not released while still rendering.
64                 std::vector<RefCountedFrame> input_frames;
65
66                 // Textures that should be released back to the resource pool
67                 // when this frame disappears, if any.
68                 // TODO: Refcount these as well?
69                 std::vector<GLuint> temp_textures;
70         };
71         // Implicitly frees the previous one if there's a new frame available.
72         bool get_display_frame(Output output, DisplayFrame *frame) {
73                 return output_channel[output].get_display_frame(frame);
74         }
75
76         typedef std::function<void()> new_frame_ready_callback_t;
77         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
78         {
79                 output_channel[output].set_frame_ready_callback(callback);
80         }
81
82 private:
83         void bm_frame(int card_index, uint16_t timecode,
84                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
85                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
86         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
87         void thread_func();
88         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
89         void release_display_frame(DisplayFrame *frame);
90
91         QSurface *mixer_surface, *h264_encoder_surface;
92         std::unique_ptr<movit::ResourcePool> resource_pool;
93         std::unique_ptr<Theme> theme;
94         std::unique_ptr<movit::EffectChain> display_chain;
95         GLuint cbcr_program_num;  // Owned by <resource_pool>.
96         std::unique_ptr<H264Encoder> h264_encoder;
97
98         // Effects part of <display_chain>. Owned by <display_chain>.
99         movit::FlatInput *display_input;
100
101         Source current_source = SOURCE_INPUT1;
102         int frame = 0;
103
104         std::mutex bmusb_mutex;
105         struct CaptureCard {
106                 BMUSBCapture *usb;
107                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
108
109                 // Threading stuff
110                 bool thread_initialized = false;
111                 QSurface *surface;
112                 QOpenGLContext *context;
113
114                 bool new_data_ready = false;  // Whether new_frame contains anything.
115                 RefCountedFrame new_frame;
116                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
117                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
118         };
119         CaptureCard cards[NUM_CARDS];  // protected by <bmusb_mutex>
120
121         RefCountedFrame bmusb_current_rendering_frame[NUM_CARDS];
122
123         class OutputChannel {
124         public:
125                 void output_frame(DisplayFrame frame);
126                 bool get_display_frame(DisplayFrame *frame);
127                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
128
129         private:
130                 friend class Mixer;
131
132                 Mixer *parent = nullptr;  // Not owned.
133                 std::mutex frame_mutex;
134                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
135                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
136                 new_frame_ready_callback_t new_frame_ready_callback;
137                 bool has_new_frame_ready_callback = false;
138         };
139         OutputChannel output_channel[NUM_OUTPUTS];
140
141         std::thread mixer_thread;
142         bool should_quit = false;
143 };
144
145 extern Mixer *global_mixer;
146
147 #endif  // !defined(_MIXER_H)