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