]> git.sesse.net Git - nageru/blob - mixer.h
Add a resampler module, as start of sound support.
[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 #include "resampler.h"
19
20 #define NUM_CARDS 2
21
22 namespace movit {
23 class YCbCrInput;
24 }
25 class QOpenGLContext;
26 class QSurfaceFormat;
27
28 class Mixer {
29 public:
30         // The surface format is used for offscreen destinations for OpenGL contexts we need.
31         Mixer(const QSurfaceFormat &format);
32         ~Mixer();
33         void start();
34         void quit();
35
36         void transition_clicked(int transition_num);
37         void channel_clicked(int preview_num);
38
39         enum Output {
40                 OUTPUT_LIVE = 0,
41                 OUTPUT_PREVIEW,
42                 OUTPUT_INPUT0,
43                 OUTPUT_INPUT1,
44                 OUTPUT_INPUT2,
45                 OUTPUT_INPUT3,
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         std::vector<std::string> get_transition_names()
82         {
83                 return theme->get_transition_names(frame / 60.0);
84         }
85
86 private:
87         void bm_frame(int card_index, uint16_t timecode,
88                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
89                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
90         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
91         void thread_func();
92         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
93         void release_display_frame(DisplayFrame *frame);
94
95         QSurface *mixer_surface, *h264_encoder_surface;
96         std::unique_ptr<movit::ResourcePool> resource_pool;
97         std::unique_ptr<Theme> theme;
98         std::unique_ptr<movit::EffectChain> display_chain;
99         GLuint cbcr_program_num;  // Owned by <resource_pool>.
100         std::unique_ptr<H264Encoder> h264_encoder;
101
102         // Effects part of <display_chain>. Owned by <display_chain>.
103         movit::FlatInput *display_input;
104
105         int frame = 0;
106
107         std::mutex bmusb_mutex;
108         struct CaptureCard {
109                 BMUSBCapture *usb;
110                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
111
112                 // Stuff for the OpenGL context (for texture uploading).
113                 QSurface *surface;
114                 QOpenGLContext *context;
115
116                 bool new_data_ready = false;  // Whether new_frame contains anything.
117                 RefCountedFrame new_frame;
118                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
119                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
120                 Resampler *resampler = nullptr;
121         };
122         CaptureCard cards[NUM_CARDS];  // protected by <bmusb_mutex>
123
124         RefCountedFrame bmusb_current_rendering_frame[NUM_CARDS];
125
126         class OutputChannel {
127         public:
128                 ~OutputChannel();
129                 void output_frame(DisplayFrame frame);
130                 bool get_display_frame(DisplayFrame *frame);
131                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
132
133         private:
134                 friend class Mixer;
135
136                 Mixer *parent = nullptr;  // Not owned.
137                 std::mutex frame_mutex;
138                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
139                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
140                 new_frame_ready_callback_t new_frame_ready_callback;
141                 bool has_new_frame_ready_callback = false;
142         };
143         OutputChannel output_channel[NUM_OUTPUTS];
144
145         std::thread mixer_thread;
146         bool should_quit = false;
147 };
148
149 extern Mixer *global_mixer;
150
151 #endif  // !defined(_MIXER_H)