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