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