]> git.sesse.net Git - nageru/blob - mixer.h
Add some primitive emergency card mapping. Useful for testing.
[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/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 #include "timebase.h"
20 #include "httpd.h"
21 #include "ebu_r128_proc.h"
22
23 namespace movit {
24 class YCbCrInput;
25 }
26 class QOpenGLContext;
27 class QSurfaceFormat;
28
29 class Mixer {
30 public:
31         // The surface format is used for offscreen destinations for OpenGL contexts we need.
32         Mixer(const QSurfaceFormat &format, unsigned num_cards);
33         ~Mixer();
34         void start();
35         void quit();
36
37         void transition_clicked(int transition_num);
38         void channel_clicked(int preview_num);
39
40         enum Output {
41                 OUTPUT_LIVE = 0,
42                 OUTPUT_PREVIEW,
43                 OUTPUT_INPUT0,
44                 OUTPUT_INPUT1,
45                 OUTPUT_INPUT2,
46                 OUTPUT_INPUT3,
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         typedef std::function<void(float, float, float, float, float)> audio_level_callback_t;
83         void set_audio_level_callback(audio_level_callback_t callback)
84         {
85                 audio_level_callback = callback;
86         }
87
88         std::vector<std::string> get_transition_names()
89         {
90                 return theme->get_transition_names(pts());
91         }
92
93 private:
94         void bm_frame(unsigned card_index, uint16_t timecode,
95                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
96                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
97         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
98         void thread_func();
99         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
100         void release_display_frame(DisplayFrame *frame);
101         double pts() { return double(pts_int) / TIMEBASE; }
102
103         HTTPD httpd;
104         unsigned num_cards;
105
106         QSurface *mixer_surface, *h264_encoder_surface;
107         std::unique_ptr<movit::ResourcePool> resource_pool;
108         std::unique_ptr<Theme> theme;
109         std::unique_ptr<movit::EffectChain> display_chain;
110         GLuint cbcr_program_num;  // Owned by <resource_pool>.
111         std::unique_ptr<H264Encoder> h264_encoder;
112
113         // Effects part of <display_chain>. Owned by <display_chain>.
114         movit::FlatInput *display_input;
115
116         int64_t pts_int = 0;  // In TIMEBASE units.
117
118         std::mutex bmusb_mutex;
119         struct CaptureCard {
120                 BMUSBCapture *usb;
121                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
122
123                 // Stuff for the OpenGL context (for texture uploading).
124                 QSurface *surface;
125                 QOpenGLContext *context;
126
127                 bool new_data_ready = false;  // Whether new_frame and new_frame_audio contains anything.
128                 bool should_quit = false;
129                 RefCountedFrame new_frame;
130                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
131                 std::vector<float> new_frame_audio;
132                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
133                 unsigned dropped_frames = 0;  // Before new_frame.
134
135                 std::mutex audio_mutex;
136                 std::unique_ptr<Resampler> resampler;  // Under audio_mutex.
137                 int last_timecode = -1;  // Unwrapped.
138         };
139         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
140
141         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
142
143         class OutputChannel {
144         public:
145                 ~OutputChannel();
146                 void output_frame(DisplayFrame frame);
147                 bool get_display_frame(DisplayFrame *frame);
148                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
149
150         private:
151                 friend class Mixer;
152
153                 Mixer *parent = nullptr;  // Not owned.
154                 std::mutex frame_mutex;
155                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
156                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
157                 new_frame_ready_callback_t new_frame_ready_callback;
158                 bool has_new_frame_ready_callback = false;
159         };
160         OutputChannel output_channel[NUM_OUTPUTS];
161
162         std::thread mixer_thread;
163         bool should_quit = false;
164
165         audio_level_callback_t audio_level_callback = nullptr;
166         Ebu_r128_proc r128;
167
168         // TODO: Implement oversampled peak detection.
169         float peak = 0.0f;
170 };
171
172 extern Mixer *global_mixer;
173
174 #endif  // !defined(_MIXER_H)