]> git.sesse.net Git - nageru/blob - mixer.h
Move all mixer stuff into a class.
[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 #include <functional>
8
9 #include "bmusb.h"
10 #include "ref_counted_gl_sync.h"
11
12 #define NUM_CARDS 2
13
14 namespace movit {
15 class Effect;
16 class ResourcePool;
17 }
18 class QOpenGLContext;
19 class QSurface;
20
21 class Mixer {
22 public:
23         void start(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4);
24         void quit();
25
26         enum Source {
27                 SOURCE_INPUT1,
28                 SOURCE_INPUT2,
29                 SOURCE_SBS,
30         };
31         void cut(Source source);
32
33         struct DisplayFrame {
34                 GLuint texnum;
35                 RefCountedGLsync ready_fence;  // Asserted when the texture is done rendering.
36         };
37         // Implicitly frees the previous one if there's a new frame available.
38         bool get_display_frame(DisplayFrame *frame);
39
40         typedef std::function<void()> new_frame_ready_callback_t;
41         void set_frame_ready_fallback(new_frame_ready_callback_t callback);
42
43 private:
44         void bm_frame(int card_index, uint16_t timecode,
45                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
46                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
47         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
48         void thread_func(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4);
49
50         Source current_source = SOURCE_INPUT1;
51
52         movit::ResourcePool *resource_pool;
53
54         std::mutex display_frame_mutex;
55         DisplayFrame current_display_frame, ready_display_frame;  // protected by <frame_mutex>
56         bool has_current_display_frame = false, has_ready_display_frame = false;  // protected by <frame_mutex>
57
58         std::mutex bmusb_mutex;
59         struct CaptureCard {
60                 BMUSBCapture *usb;
61
62                 // Threading stuff
63                 bool thread_initialized;
64                 QSurface *surface;
65                 QOpenGLContext *context;
66
67                 bool new_data_ready;  // Whether new_frame contains anything.
68                 FrameAllocator::Frame new_frame;
69                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
70                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
71         };
72         CaptureCard cards[NUM_CARDS];  // protected by <bmusb_mutex>
73
74         new_frame_ready_callback_t new_frame_ready_callback;
75         bool has_new_frame_ready_callback = false;
76
77         std::thread mixer_thread;
78         bool should_quit;
79 };
80
81 extern Mixer *global_mixer;
82
83 #endif  // !defined(_MIXER_H)