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