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