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