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