]> git.sesse.net Git - nageru/blob - mixer.cpp
Run IWYU (plus lots of manual fiddling).
[nageru] / mixer.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3 #define NUM_CARDS 2
4
5 #define WIDTH 1280
6 #define HEIGHT 720
7
8 #undef Success
9
10 #include "mixer.h"
11
12 #include <assert.h>
13 #include <effect.h>
14 #include <effect_chain.h>
15 #include <effect_util.h>
16 #include <epoxy/egl.h>
17 #include <features.h>
18 #include <image_format.h>
19 #include <init.h>
20 #include <overlay_effect.h>
21 #include <padding_effect.h>
22 #include <resample_effect.h>
23 #include <resource_pool.h>
24 #include <saturation_effect.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/time.h>
29 #include <time.h>
30 #include <util.h>
31 #include <white_balance_effect.h>
32 #include <ycbcr.h>
33 #include <ycbcr_input.h>
34 #include <cmath>
35 #include <condition_variable>
36 #include <cstddef>
37 #include <memory>
38 #include <mutex>
39 #include <string>
40 #include <thread>
41 #include <vector>
42
43 #include "bmusb.h"
44 #include "context.h"
45 #include "h264encode.h"
46 #include "pbo_frame_allocator.h"
47 #include "ref_counted_gl_sync.h"
48
49 class QOpenGLContext;
50
51 using namespace movit;
52 using namespace std;
53 using namespace std::placeholders;
54
55 Source current_source = SOURCE_INPUT1;
56
57 ResourcePool *resource_pool;
58
59 std::mutex display_frame_mutex;
60 DisplayFrame current_display_frame, ready_display_frame;  // protected by <frame_mutex>
61 bool has_current_display_frame = false, has_ready_display_frame = false;  // protected by <frame_mutex>
62
63 std::mutex bmusb_mutex;
64 struct CaptureCard {
65         BMUSBCapture *usb;
66
67         // Threading stuff
68         bool thread_initialized;
69         QSurface *surface;
70         QOpenGLContext *context;
71
72         bool new_data_ready;  // Whether new_frame contains anything.
73         PBOFrameAllocator::Frame new_frame;
74         GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
75         std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
76 };
77 CaptureCard cards[NUM_CARDS];  // protected by <bmusb_mutex>
78
79 new_frame_ready_callback_t new_frame_ready_callback;
80 bool has_new_frame_ready_callback = false;
81
82 void bm_frame(int card_index, uint16_t timecode,
83               FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
84               FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
85 {
86         CaptureCard *card = &cards[card_index];
87         if (!card->thread_initialized) {
88                 printf("initializing context for bmusb thread %d\n", card_index);
89                 eglBindAPI(EGL_OPENGL_API);
90                 card->context = create_context();
91                 if (!make_current(card->context, card->surface)) {
92                         printf("failed to create bmusb context\n");
93                         exit(1);
94                 }
95                 card->thread_initialized = true;
96         }       
97
98         if (video_frame.len - video_offset != 1280 * 750 * 2) {
99                 printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
100                 FILE *fp = fopen("frame.raw", "wb");
101                 fwrite(video_frame.data, video_frame.len, 1, fp);
102                 fclose(fp);
103                 //exit(1);
104                 card->usb->get_video_frame_allocator()->release_frame(video_frame);
105                 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
106                 return;
107         }
108         {
109                 // Wait until the previous frame was consumed.
110                 std::unique_lock<std::mutex> lock(bmusb_mutex);
111                 card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
112         }
113         GLuint pbo = (GLint)(intptr_t)video_frame.userdata;
114         check_error();
115         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
116         check_error();
117         glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size);
118         check_error();
119         //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
120         //check_error();
121         GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);              
122         check_error();
123         assert(fence != nullptr);
124         {
125                 std::unique_lock<std::mutex> lock(bmusb_mutex);
126                 card->new_data_ready = true;
127                 card->new_frame = video_frame;
128                 card->new_data_ready_fence = fence;
129                 card->new_data_ready_changed.notify_all();
130         }
131
132         // Video frame will be released later.
133         card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
134 }
135         
136 void place_rectangle(Effect *resample_effect, Effect *padding_effect, float x0, float y0, float x1, float y1)
137 {
138         float srcx0 = 0.0f;
139         float srcx1 = 1.0f;
140         float srcy0 = 0.0f;
141         float srcy1 = 1.0f;
142
143         // Cull.
144         if (x0 > 1280.0 || x1 < 0.0 || y0 > 720.0 || y1 < 0.0) {
145                 CHECK(resample_effect->set_int("width", 1));
146                 CHECK(resample_effect->set_int("height", 1));
147                 CHECK(resample_effect->set_float("zoom_x", 1280.0));
148                 CHECK(resample_effect->set_float("zoom_y", 720.0));
149                 CHECK(padding_effect->set_int("left", 2000));
150                 CHECK(padding_effect->set_int("top", 2000));
151                 return; 
152         }
153
154         // Clip. (TODO: Clip on upper/left sides, too.)
155         if (x1 > 1280.0) {
156                 srcx1 = (1280.0 - x0) / (x1 - x0);
157                 x1 = 1280.0;
158         }
159         if (y1 > 720.0) {
160                 srcy1 = (720.0 - y0) / (y1 - y0);
161                 y1 = 720.0;
162         }
163
164         float x_subpixel_offset = x0 - floor(x0);
165         float y_subpixel_offset = y0 - floor(y0);
166
167         // Resampling must be to an integral number of pixels. Round up,
168         // and then add an extra pixel so we have some leeway for the border.
169         int width = int(ceil(x1 - x0)) + 1;
170         int height = int(ceil(y1 - y0)) + 1;
171         CHECK(resample_effect->set_int("width", width));
172         CHECK(resample_effect->set_int("height", height));
173
174         // Correct the discrepancy with zoom. (This will leave a small
175         // excess edge of pixels and subpixels, which we'll correct for soon.)
176         float zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
177         float zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
178         CHECK(resample_effect->set_float("zoom_x", zoom_x));
179         CHECK(resample_effect->set_float("zoom_y", zoom_y));
180         CHECK(resample_effect->set_float("zoom_center_x", 0.0f));
181         CHECK(resample_effect->set_float("zoom_center_y", 0.0f));
182
183         // Padding must also be to a whole-pixel offset.
184         CHECK(padding_effect->set_int("left", floor(x0)));
185         CHECK(padding_effect->set_int("top", floor(y0)));
186
187         // Correct _that_ discrepancy by subpixel offset in the resampling.
188         CHECK(resample_effect->set_float("left", -x_subpixel_offset / zoom_x));
189         CHECK(resample_effect->set_float("top", -y_subpixel_offset / zoom_y));
190
191         // Finally, adjust the border so it is exactly where we want it.
192         CHECK(padding_effect->set_float("border_offset_left", x_subpixel_offset));
193         CHECK(padding_effect->set_float("border_offset_right", x1 - (floor(x0) + width)));
194         CHECK(padding_effect->set_float("border_offset_top", y_subpixel_offset));
195         CHECK(padding_effect->set_float("border_offset_bottom", y1 - (floor(y0) + height)));
196 }
197         
198 static bool quit = false;
199         
200 void mixer_thread_func(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
201 {
202         cards[0].surface = surface3;
203 #if NUM_CARDS == 2
204         cards[1].surface = surface4;
205 #endif
206
207         eglBindAPI(EGL_OPENGL_API);
208         QOpenGLContext *context = create_context();
209         if (!make_current(context, surface)) {
210                 printf("oops\n");
211                 exit(1);
212         }
213
214         CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
215         check_error();
216
217         EffectChain chain(WIDTH, HEIGHT);
218         check_error();
219
220         ImageFormat inout_format;
221         inout_format.color_space = COLORSPACE_sRGB;
222         inout_format.gamma_curve = GAMMA_sRGB;
223
224         YCbCrFormat ycbcr_format;
225         ycbcr_format.chroma_subsampling_x = 2;
226         ycbcr_format.chroma_subsampling_y = 1;
227         ycbcr_format.cb_x_position = 0.0;
228         ycbcr_format.cr_x_position = 0.0;
229         ycbcr_format.cb_y_position = 0.5;
230         ycbcr_format.cr_y_position = 0.5;
231         ycbcr_format.luma_coefficients = YCBCR_REC_601;
232         ycbcr_format.full_range = false;
233
234         YCbCrInput *input[NUM_CARDS];
235
236         input[0] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
237         chain.add_input(input[0]);
238         input[1] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
239         chain.add_input(input[1]);
240         Effect *resample_effect = chain.add_effect(new ResampleEffect(), input[0]);
241         Effect *padding_effect = chain.add_effect(new IntegralPaddingEffect());
242         float border_color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
243         CHECK(padding_effect->set_vec4("border_color", border_color));
244
245         Effect *resample2_effect = chain.add_effect(new ResampleEffect(), input[1]);
246         Effect *saturation_effect = chain.add_effect(new SaturationEffect());
247         CHECK(saturation_effect->set_float("saturation", 0.3f));
248         Effect *wb_effect = chain.add_effect(new WhiteBalanceEffect());
249         CHECK(wb_effect->set_float("output_color_temperature", 3500.0));
250         Effect *padding2_effect = chain.add_effect(new IntegralPaddingEffect());
251
252         chain.add_effect(new OverlayEffect(), padding_effect, padding2_effect);
253
254         ycbcr_format.chroma_subsampling_x = 1;
255
256         chain.add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
257         chain.add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
258         chain.set_dither_bits(8);
259         chain.set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
260         chain.finalize();
261
262         H264Encoder h264_encoder(surface2, WIDTH, HEIGHT, "test.mp4");
263
264         printf("Configuring first card...\n");
265         cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b);  // 0xbd4f
266         //cards[0].usb = new BMUSBCapture(0x1edb, 0xbd4f);
267         cards[0].usb->set_frame_callback(std::bind(bm_frame, 0, _1, _2, _3, _4, _5, _6, _7));
268         std::unique_ptr<PBOFrameAllocator> pbo_allocator1(new PBOFrameAllocator(1280 * 750 * 2 + 44));
269         cards[0].usb->set_video_frame_allocator(pbo_allocator1.get());
270         cards[0].usb->configure_card();
271
272         std::unique_ptr<PBOFrameAllocator> pbo_allocator2(new PBOFrameAllocator(1280 * 750 * 2 + 44));
273         if (NUM_CARDS == 2) {
274                 printf("Configuring second card...\n");
275                 cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
276                 cards[1].usb->set_frame_callback(std::bind(bm_frame, 1, _1, _2, _3, _4, _5, _6, _7));
277                 cards[1].usb->set_video_frame_allocator(pbo_allocator2.get());
278                 cards[1].usb->configure_card();
279         }
280
281         BMUSBCapture::start_bm_thread();
282
283         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
284                 cards[card_index].usb->start_bm_capture();
285         }
286
287         int frame = 0;
288 #if _POSIX_C_SOURCE >= 199309L
289         struct timespec start, now;
290         clock_gettime(CLOCK_MONOTONIC, &start);
291 #else
292         struct timeval start, now;
293         gettimeofday(&start, NULL);
294 #endif
295
296         PBOFrameAllocator::Frame bmusb_current_rendering_frame[NUM_CARDS];
297         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
298                 bmusb_current_rendering_frame[card_index] =
299                         cards[card_index].usb->get_video_frame_allocator()->alloc_frame();
300                 GLint input_tex_pbo = (GLint)(intptr_t)bmusb_current_rendering_frame[card_index].userdata;
301                 input[card_index]->set_pixel_data(0, nullptr, input_tex_pbo);
302                 input[card_index]->set_pixel_data(1, nullptr, input_tex_pbo);
303         }
304
305         //chain.enable_phase_timing(true);
306
307         // Set up stuff for NV12 conversion.
308         resource_pool = chain.get_resource_pool();
309         GLuint chroma_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
310
311         // Cb/Cr shader.
312         string cbcr_vert_shader = read_file("vs-cbcr.130.vert");
313         string cbcr_frag_shader =
314                 "#version 130 \n"
315                 "in vec2 tc0; \n"
316                 "uniform sampler2D cbcr_tex; \n"
317                 "void main() { \n"
318                 "    gl_FragColor = texture2D(cbcr_tex, tc0); \n"
319                 "} \n";
320         GLuint cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
321
322         GLuint vao;
323         glGenVertexArrays(1, &vao);
324         check_error();
325
326         while (!quit) {
327                 ++frame;
328
329                 //int width0 = lrintf(848 * (1.0 + 0.2 * sin(frame * 0.02)));
330                 int width0 = 848;
331                 int height0 = lrintf(width0 * 9.0 / 16.0);
332
333                 //float top0 = 96 + 48 * sin(frame * 0.005);
334                 //float left0 = 96 + 48 * cos(frame * 0.006);
335                 float top0 = 48;
336                 float left0 = 16;
337                 float bottom0 = top0 + height0;
338                 float right0 = left0 + width0;
339
340                 int width1 = 384;
341                 int height1 = 216;
342         
343                 float bottom1 = 720 - 48;
344                 float right1 = 1280 - 16;
345                 float top1 = bottom1 - height1;
346                 float left1 = right1 - width1;
347         
348                 if (current_source == SOURCE_INPUT1) {
349                         top0 = 0.0;
350                         bottom0 = HEIGHT;
351                         left0 = 0.0;
352                         right0 = WIDTH;
353
354                         top1 = HEIGHT + 10;
355                         bottom1 = HEIGHT + 20;
356                         left1 = WIDTH + 10;
357                         right1 = WIDTH + 20;
358                 } else if (current_source == SOURCE_INPUT2) {
359                         top1 = 0.0;
360                         bottom1 = HEIGHT;
361                         left1 = 0.0;
362                         right1 = WIDTH;
363
364                         top0 = HEIGHT + 10;
365                         bottom0 = HEIGHT + 20;
366                         left0 = WIDTH + 10;
367                         right0 = WIDTH + 20;
368                 } else {
369                         float t = 0.5 + 0.5 * cos(frame * 0.006);
370                         float scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
371                         float tx0 = 0.0 + t * (-16.0 * scale0);
372                         float ty0 = 0.0 + t * (-48.0 * scale0);
373
374                         top0 = top0 * scale0 + ty0;
375                         bottom0 = bottom0 * scale0 + ty0;
376                         left0 = left0 * scale0 + tx0;
377                         right0 = right0 * scale0 + tx0;
378
379                         top1 = top1 * scale0 + ty0;
380                         bottom1 = bottom1 * scale0 + ty0;
381                         left1 = left1 * scale0 + tx0;
382                         right1 = right1 * scale0 + tx0;
383                 }
384
385                 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
386                 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);
387
388                 CaptureCard card_copy[NUM_CARDS];
389
390                 {
391                         std::unique_lock<std::mutex> lock(bmusb_mutex);
392
393                         // The first card is the master timer, so wait for it to have a new frame.
394                         // TODO: Make configurable, and with a timeout.
395                         cards[0].new_data_ready_changed.wait(lock, []{ return cards[0].new_data_ready; });
396
397                         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
398                                 CaptureCard *card = &cards[card_index];
399                                 card_copy[card_index].usb = card->usb;
400                                 card_copy[card_index].new_data_ready = card->new_data_ready;
401                                 card_copy[card_index].new_frame = card->new_frame;
402                                 card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence;
403                                 card->new_data_ready = false;
404                                 card->new_data_ready_changed.notify_all();
405                         }
406                 }
407
408                 vector<FrameAllocator::Frame> input_frames_to_release;
409         
410                 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
411                         CaptureCard *card = &card_copy[card_index];
412                         if (!card->new_data_ready)
413                                 continue;
414
415                         // Now we're done with the previous frame, so we can definitely
416                         // release it when this is done rendering. (Actually, we could do
417                         // it one frame earlier, but before we have a new one, there's no
418                         // knowing when the current one is released.)
419                         input_frames_to_release.push_back(bmusb_current_rendering_frame[card_index]);
420                         bmusb_current_rendering_frame[card_index] = card->new_frame;
421                         check_error();
422
423                         // The new texture might still be uploaded,
424                         // tell the GPU to wait until it's there.
425                         if (card->new_data_ready_fence)
426                                 glWaitSync(card->new_data_ready_fence, /*flags=*/0, GL_TIMEOUT_IGNORED);
427                         check_error();
428                         glDeleteSync(card->new_data_ready_fence);
429                         check_error();
430                         GLint input_tex_pbo = (GLint)(intptr_t)card->new_frame.userdata;
431                         input[card_index]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
432                         input[card_index]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
433
434                         if (NUM_CARDS == 1) {
435                                 // Set to the other one, too.
436                                 input[1]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
437                                 input[1]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
438                         }
439                 }
440
441                 GLuint y_tex, cbcr_tex;
442                 bool got_frame = h264_encoder.begin_frame(&y_tex, &cbcr_tex);
443                 assert(got_frame);
444
445                 // Render chain.
446                 GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGBA8, WIDTH, HEIGHT);
447                 GLuint ycbcr_fbo = resource_pool->create_fbo(y_tex, chroma_tex, rgba_tex);
448                 chain.render_to_fbo(ycbcr_fbo, WIDTH, HEIGHT);
449                 resource_pool->release_fbo(ycbcr_fbo);
450
451                 // Set up for extraction.
452                 float vertices[] = {
453                         0.0f, 2.0f,
454                         0.0f, 0.0f,
455                         2.0f, 0.0f
456                 };
457
458                 glBindVertexArray(vao);
459                 check_error();
460
461                 // Extract Cb/Cr.
462                 GLuint cbcr_fbo = resource_pool->create_fbo(cbcr_tex);
463                 glBindFramebuffer(GL_FRAMEBUFFER, cbcr_fbo);
464                 glViewport(0, 0, WIDTH/2, HEIGHT/2);
465                 check_error();
466
467                 glUseProgram(cbcr_program_num);
468                 check_error();
469
470                 glActiveTexture(GL_TEXTURE0);
471                 check_error();
472                 glBindTexture(GL_TEXTURE_2D, chroma_tex);
473                 check_error();
474                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
475                 check_error();
476                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
477                 check_error();
478                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
479                 check_error();
480
481                 float chroma_offset_0[] = { -0.5f / WIDTH, 0.0f };
482                 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
483
484                 GLuint position_vbo = fill_vertex_attribute(cbcr_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
485                 GLuint texcoord_vbo = fill_vertex_attribute(cbcr_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
486
487                 glDrawArrays(GL_TRIANGLES, 0, 3);
488                 check_error();
489
490                 RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
491                 check_error();
492
493                 cleanup_vertex_attribute(cbcr_program_num, "position", position_vbo);
494                 cleanup_vertex_attribute(cbcr_program_num, "texcoord", texcoord_vbo);
495
496                 glUseProgram(0);
497                 check_error();
498
499                 resource_pool->release_fbo(cbcr_fbo);
500
501                 h264_encoder.end_frame(fence, input_frames_to_release);
502
503                 // Store this frame for display. Remove the ready frame if any
504                 // (it was seemingly never used).
505                 {
506                         std::unique_lock<std::mutex> lock(display_frame_mutex);
507                         if (has_ready_display_frame) {
508                                 resource_pool->release_2d_texture(ready_display_frame.texnum);
509                                 ready_display_frame.ready_fence.reset();
510                         }
511                         ready_display_frame.texnum = rgba_tex;
512                         ready_display_frame.ready_fence = fence;
513                         has_ready_display_frame = true;
514                 }
515
516                 if (has_new_frame_ready_callback) {
517                         new_frame_ready_callback();
518                 }
519
520 #if 1
521 #if _POSIX_C_SOURCE >= 199309L
522                 clock_gettime(CLOCK_MONOTONIC, &now);
523                 double elapsed = now.tv_sec - start.tv_sec +
524                         1e-9 * (now.tv_nsec - start.tv_nsec);
525 #else
526                 gettimeofday(&now, NULL);
527                 double elapsed = now.tv_sec - start.tv_sec +
528                         1e-6 * (now.tv_usec - start.tv_usec);
529 #endif
530                 if (frame % 100 == 0) {
531                         printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
532                                 frame, elapsed, frame / elapsed,
533                                 1e3 * elapsed / frame);
534                 //      chain.print_phase_timing();
535                 }
536
537                 // Reset every 100 frames, so that local variations in frame times
538                 // (especially for the first few frames, when the shaders are
539                 // compiled etc.) don't make it hard to measure for the entire
540                 // remaining duration of the program.
541                 if (frame == 10000) {
542                         frame = 0;
543                         start = now;
544                 }
545 #endif
546                 check_error();
547         }
548         glDeleteVertexArrays(1, &vao);
549         resource_pool->release_glsl_program(cbcr_program_num);
550         resource_pool->release_2d_texture(chroma_tex);
551         BMUSBCapture::stop_bm_thread();
552 }
553
554 bool mixer_get_display_frame(DisplayFrame *frame)
555 {
556         std::unique_lock<std::mutex> lock(display_frame_mutex);
557         if (!has_current_display_frame && !has_ready_display_frame) {
558                 return false;
559         }
560
561         if (has_current_display_frame && has_ready_display_frame) {
562                 // We have a new ready frame. Toss the current one.
563                 resource_pool->release_2d_texture(current_display_frame.texnum);
564                 current_display_frame.ready_fence.reset();
565                 has_current_display_frame = false;
566         }
567         if (has_ready_display_frame) {
568                 assert(!has_current_display_frame);
569                 current_display_frame = ready_display_frame;
570                 ready_display_frame.ready_fence.reset();  // Drop the refcount.
571                 has_current_display_frame = true;
572                 has_ready_display_frame = false;
573         }
574
575         *frame = current_display_frame;
576         return true;
577 }
578
579 void set_frame_ready_fallback(new_frame_ready_callback_t callback)
580 {
581         new_frame_ready_callback = callback;
582         has_new_frame_ready_callback = true;
583 }
584
585 std::thread mixer_thread;
586
587 void start_mixer(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
588 {
589         mixer_thread = std::thread([surface, surface2, surface3, surface4]{
590                 mixer_thread_func(surface, surface2, surface3, surface4);
591         });
592 }
593
594 void mixer_quit()
595 {
596         quit = true;
597         mixer_thread.join();
598 }
599
600 void mixer_cut(Source source)
601 {
602         current_source = source;
603 }