]> git.sesse.net Git - nageru/blob - mixer.cpp
Ask the Lua script for number of channels.
[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 Mixer::Mixer(const QSurfaceFormat &format)
54         : mixer_surface(create_surface(format)),
55           h264_encoder_surface(create_surface(format))
56 {
57         CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
58         check_error();
59
60         resource_pool.reset(new ResourcePool);
61         theme.reset(new Theme("theme.lua", resource_pool.get()));
62         output_channel[OUTPUT_LIVE].parent = this;
63         output_channel[OUTPUT_PREVIEW].parent = this;
64         output_channel[OUTPUT_INPUT0].parent = this;
65         output_channel[OUTPUT_INPUT1].parent = this;
66
67         ImageFormat inout_format;
68         inout_format.color_space = COLORSPACE_sRGB;
69         inout_format.gamma_curve = GAMMA_sRGB;
70
71         // Display chain; shows the live output produced by the main chain (its RGBA version).
72         display_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get()));
73         check_error();
74         display_input = new FlatInput(inout_format, FORMAT_RGB, GL_UNSIGNED_BYTE, WIDTH, HEIGHT);  // FIXME: GL_UNSIGNED_BYTE is really wrong.
75         display_chain->add_input(display_input);
76         display_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
77         display_chain->set_dither_bits(0);  // Don't bother.
78         display_chain->finalize();
79
80         h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mp4"));
81
82         printf("Configuring first card...\n");
83         cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b);  // 0xbd4f
84         cards[0].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 0, _1, _2, _3, _4, _5, _6, _7));
85         cards[0].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
86         cards[0].usb->set_video_frame_allocator(cards[0].frame_allocator.get());
87         cards[0].usb->configure_card();
88         cards[0].surface = create_surface(format);
89 #if NUM_CARDS == 2
90         cards[1].surface = create_surface(format);
91 #endif
92
93         if (NUM_CARDS == 2) {
94                 printf("Configuring second card...\n");
95                 cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
96                 cards[1].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 1, _1, _2, _3, _4, _5, _6, _7));
97                 cards[1].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
98                 cards[1].usb->set_video_frame_allocator(cards[1].frame_allocator.get());
99                 cards[1].usb->configure_card();
100         }
101
102         BMUSBCapture::start_bm_thread();
103
104         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
105                 cards[card_index].usb->start_bm_capture();
106         }
107
108         //chain->enable_phase_timing(true);
109
110         // Set up stuff for NV12 conversion.
111
112         // Cb/Cr shader.
113         string cbcr_vert_shader = read_file("vs-cbcr.130.vert");
114         string cbcr_frag_shader =
115                 "#version 130 \n"
116                 "in vec2 tc0; \n"
117                 "uniform sampler2D cbcr_tex; \n"
118                 "void main() { \n"
119                 "    gl_FragColor = texture2D(cbcr_tex, tc0); \n"
120                 "} \n";
121         cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
122 }
123
124 Mixer::~Mixer()
125 {
126         resource_pool->release_glsl_program(cbcr_program_num);
127         BMUSBCapture::stop_bm_thread();
128 }
129
130 void Mixer::bm_frame(int card_index, uint16_t timecode,
131                      FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
132                      FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
133 {
134         CaptureCard *card = &cards[card_index];
135         if (!card->thread_initialized) {
136                 printf("initializing context for bmusb thread %d\n", card_index);
137                 eglBindAPI(EGL_OPENGL_API);
138                 card->context = create_context();
139                 if (!make_current(card->context, card->surface)) {
140                         printf("failed to create bmusb context\n");
141                         exit(1);
142                 }
143                 card->thread_initialized = true;
144         }       
145
146         if (video_frame.len - video_offset != 1280 * 750 * 2) {
147                 printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
148                 FILE *fp = fopen("frame.raw", "wb");
149                 fwrite(video_frame.data, video_frame.len, 1, fp);
150                 fclose(fp);
151                 //exit(1);
152                 card->usb->get_video_frame_allocator()->release_frame(video_frame);
153                 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
154                 return;
155         }
156         {
157                 // Wait until the previous frame was consumed.
158                 std::unique_lock<std::mutex> lock(bmusb_mutex);
159                 card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
160         }
161         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)video_frame.userdata;
162         GLuint pbo = userdata->pbo;
163         check_error();
164         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
165         check_error();
166         glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size);
167         check_error();
168         //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
169         //check_error();
170
171         // Upload the textures.
172         glBindTexture(GL_TEXTURE_2D, userdata->tex_y);
173         check_error();
174         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1280, 720, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22));
175         check_error();
176         glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr);
177         check_error();
178         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1280/2, 720, GL_RG, GL_UNSIGNED_BYTE, BUFFER_OFFSET(1280 * 25 + 22));
179         check_error();
180         glBindTexture(GL_TEXTURE_2D, 0);
181         check_error();
182         GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);              
183         check_error();
184         assert(fence != nullptr);
185         {
186                 std::unique_lock<std::mutex> lock(bmusb_mutex);
187                 card->new_data_ready = true;
188                 card->new_frame = RefCountedFrame(video_frame);
189                 card->new_data_ready_fence = fence;
190                 card->new_data_ready_changed.notify_all();
191         }
192
193         // Video frame will be released when last user of card->new_frame goes out of scope.
194         card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
195 }
196         
197 void Mixer::place_rectangle(Effect *resample_effect, Effect *padding_effect, float x0, float y0, float x1, float y1)
198 {
199         float srcx0 = 0.0f;
200         float srcx1 = 1.0f;
201         float srcy0 = 0.0f;
202         float srcy1 = 1.0f;
203
204         // Cull.
205         if (x0 > 1280.0 || x1 < 0.0 || y0 > 720.0 || y1 < 0.0) {
206                 CHECK(resample_effect->set_int("width", 1));
207                 CHECK(resample_effect->set_int("height", 1));
208                 CHECK(resample_effect->set_float("zoom_x", 1280.0));
209                 CHECK(resample_effect->set_float("zoom_y", 720.0));
210                 CHECK(padding_effect->set_int("left", 2000));
211                 CHECK(padding_effect->set_int("top", 2000));
212                 return; 
213         }
214
215         // Clip. (TODO: Clip on upper/left sides, too.)
216         if (x1 > 1280.0) {
217                 srcx1 = (1280.0 - x0) / (x1 - x0);
218                 x1 = 1280.0;
219         }
220         if (y1 > 720.0) {
221                 srcy1 = (720.0 - y0) / (y1 - y0);
222                 y1 = 720.0;
223         }
224
225         float x_subpixel_offset = x0 - floor(x0);
226         float y_subpixel_offset = y0 - floor(y0);
227
228         // Resampling must be to an integral number of pixels. Round up,
229         // and then add an extra pixel so we have some leeway for the border.
230         int width = int(ceil(x1 - x0)) + 1;
231         int height = int(ceil(y1 - y0)) + 1;
232         CHECK(resample_effect->set_int("width", width));
233         CHECK(resample_effect->set_int("height", height));
234
235         // Correct the discrepancy with zoom. (This will leave a small
236         // excess edge of pixels and subpixels, which we'll correct for soon.)
237         float zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
238         float zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
239         CHECK(resample_effect->set_float("zoom_x", zoom_x));
240         CHECK(resample_effect->set_float("zoom_y", zoom_y));
241         CHECK(resample_effect->set_float("zoom_center_x", 0.0f));
242         CHECK(resample_effect->set_float("zoom_center_y", 0.0f));
243
244         // Padding must also be to a whole-pixel offset.
245         CHECK(padding_effect->set_int("left", floor(x0)));
246         CHECK(padding_effect->set_int("top", floor(y0)));
247
248         // Correct _that_ discrepancy by subpixel offset in the resampling.
249         CHECK(resample_effect->set_float("left", -x_subpixel_offset / zoom_x));
250         CHECK(resample_effect->set_float("top", -y_subpixel_offset / zoom_y));
251
252         // Finally, adjust the border so it is exactly where we want it.
253         CHECK(padding_effect->set_float("border_offset_left", x_subpixel_offset));
254         CHECK(padding_effect->set_float("border_offset_right", x1 - (floor(x0) + width)));
255         CHECK(padding_effect->set_float("border_offset_top", y_subpixel_offset));
256         CHECK(padding_effect->set_float("border_offset_bottom", y1 - (floor(y0) + height)));
257 }
258         
259 void Mixer::thread_func()
260 {
261         eglBindAPI(EGL_OPENGL_API);
262         QOpenGLContext *context = create_context();
263         if (!make_current(context, mixer_surface)) {
264                 printf("oops\n");
265                 exit(1);
266         }
267
268         struct timespec start, now;
269         clock_gettime(CLOCK_MONOTONIC, &start);
270
271         while (!should_quit) {
272                 ++frame;
273
274 #if 0
275                 //int width0 = lrintf(848 * (1.0 + 0.2 * sin(frame * 0.02)));
276                 int width0 = 848;
277                 int height0 = lrintf(width0 * 9.0 / 16.0);
278
279                 //float top0 = 96 + 48 * sin(frame * 0.005);
280                 //float left0 = 96 + 48 * cos(frame * 0.006);
281                 float top0 = 48;
282                 float left0 = 16;
283                 float bottom0 = top0 + height0;
284                 float right0 = left0 + width0;
285
286                 int width1 = 384;
287                 int height1 = 216;
288         
289                 float bottom1 = 720 - 48;
290                 float right1 = 1280 - 16;
291                 float top1 = bottom1 - height1;
292                 float left1 = right1 - width1;
293         
294                 if (current_source == SOURCE_INPUT1) {
295                         top0 = 0.0;
296                         bottom0 = HEIGHT;
297                         left0 = 0.0;
298                         right0 = WIDTH;
299
300                         top1 = HEIGHT + 10;
301                         bottom1 = HEIGHT + 20;
302                         left1 = WIDTH + 10;
303                         right1 = WIDTH + 20;
304                 } else if (current_source == SOURCE_INPUT2) {
305                         top1 = 0.0;
306                         bottom1 = HEIGHT;
307                         left1 = 0.0;
308                         right1 = WIDTH;
309
310                         top0 = HEIGHT + 10;
311                         bottom0 = HEIGHT + 20;
312                         left0 = WIDTH + 10;
313                         right0 = WIDTH + 20;
314                 } else {
315                         float t = 0.5 + 0.5 * cos(frame * 0.006);
316                         float scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
317                         float tx0 = 0.0 + t * (-16.0 * scale0);
318                         float ty0 = 0.0 + t * (-48.0 * scale0);
319
320                         top0 = top0 * scale0 + ty0;
321                         bottom0 = bottom0 * scale0 + ty0;
322                         left0 = left0 * scale0 + tx0;
323                         right0 = right0 * scale0 + tx0;
324
325                         top1 = top1 * scale0 + ty0;
326                         bottom1 = bottom1 * scale0 + ty0;
327                         left1 = left1 * scale0 + tx0;
328                         right1 = right1 * scale0 + tx0;
329                 }
330
331                 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
332                 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);
333 #endif
334
335                 CaptureCard card_copy[NUM_CARDS];
336
337                 {
338                         std::unique_lock<std::mutex> lock(bmusb_mutex);
339
340                         // The first card is the master timer, so wait for it to have a new frame.
341                         // TODO: Make configurable, and with a timeout.
342                         cards[0].new_data_ready_changed.wait(lock, [this]{ return cards[0].new_data_ready; });
343
344                         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
345                                 CaptureCard *card = &cards[card_index];
346                                 card_copy[card_index].usb = card->usb;
347                                 card_copy[card_index].new_data_ready = card->new_data_ready;
348                                 card_copy[card_index].new_frame = card->new_frame;
349                                 card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence;
350                                 card->new_data_ready = false;
351                                 card->new_data_ready_changed.notify_all();
352                         }
353                 }
354
355                 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
356                         CaptureCard *card = &card_copy[card_index];
357                         if (!card->new_data_ready)
358                                 continue;
359
360                         assert(card->new_frame != nullptr);
361                         bmusb_current_rendering_frame[card_index] = card->new_frame;
362                         check_error();
363
364                         // The new texture might still be uploaded,
365                         // tell the GPU to wait until it's there.
366                         if (card->new_data_ready_fence)
367                                 glWaitSync(card->new_data_ready_fence, /*flags=*/0, GL_TIMEOUT_IGNORED);
368                         check_error();
369                         glDeleteSync(card->new_data_ready_fence);
370                         check_error();
371                         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)card->new_frame->userdata;
372                         theme->set_input_textures(card_index, userdata->tex_y, userdata->tex_cbcr);
373                 }
374
375                 // Get the main chain from the theme, and set its state immediately.
376                 pair<EffectChain *, function<void()>> theme_main_chain = theme->get_chain(0, frame / 60.0f, WIDTH, HEIGHT);
377                 EffectChain *chain = theme_main_chain.first;
378                 theme_main_chain.second();
379
380                 GLuint y_tex, cbcr_tex;
381                 bool got_frame = h264_encoder->begin_frame(&y_tex, &cbcr_tex);
382                 assert(got_frame);
383
384                 // Render main chain.
385                 GLuint cbcr_full_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
386                 GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGB565, WIDTH, HEIGHT);  // Saves texture bandwidth, although dithering gets messed up.
387                 GLuint fbo = resource_pool->create_fbo(y_tex, cbcr_full_tex, rgba_tex);
388                 chain->render_to_fbo(fbo, WIDTH, HEIGHT);
389                 resource_pool->release_fbo(fbo);
390
391                 subsample_chroma(cbcr_full_tex, cbcr_tex);
392                 resource_pool->release_2d_texture(cbcr_full_tex);
393
394                 // Set the right state for rgba_tex.
395                 glBindFramebuffer(GL_FRAMEBUFFER, 0);
396                 glBindTexture(GL_TEXTURE_2D, rgba_tex);
397                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
398                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
399                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
400
401                 RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
402                 check_error();
403
404                 // Make sure the H.264 gets a reference to all the
405                 // input frames needed, so that they are not released back
406                 // until the rendering is done.
407                 vector<RefCountedFrame> input_frames;
408                 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
409                         input_frames.push_back(bmusb_current_rendering_frame[card_index]);
410                 }
411                 h264_encoder->end_frame(fence, input_frames);
412
413                 // The live frame just shows the RGBA texture we just rendered.
414                 // It owns rgba_tex now.
415                 DisplayFrame live_frame;
416                 live_frame.chain = display_chain.get();
417                 live_frame.setup_chain = [this, rgba_tex]{
418                         display_input->set_texture_num(rgba_tex);
419                 };
420                 live_frame.ready_fence = fence;
421                 live_frame.input_frames = {};
422                 live_frame.temp_textures = { rgba_tex };
423                 output_channel[OUTPUT_LIVE].output_frame(live_frame);
424
425                 // Set up preview and any additional channels.
426                 for (unsigned i = 1; i < theme->get_num_channels() + 2; ++i) {
427                         DisplayFrame display_frame;
428                         pair<EffectChain *, function<void()>> chain = theme->get_chain(i, frame / 60.0f, WIDTH, HEIGHT);  // FIXME: dimensions
429                         display_frame.chain = chain.first;
430                         display_frame.setup_chain = chain.second;
431                         display_frame.ready_fence = fence;
432                         display_frame.input_frames = { bmusb_current_rendering_frame[0], bmusb_current_rendering_frame[1] };  // FIXME: possible to do better?
433                         display_frame.temp_textures = {};
434                         output_channel[i].output_frame(display_frame);
435                 }
436
437                 clock_gettime(CLOCK_MONOTONIC, &now);
438                 double elapsed = now.tv_sec - start.tv_sec +
439                         1e-9 * (now.tv_nsec - start.tv_nsec);
440                 if (frame % 100 == 0) {
441                         printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
442                                 frame, elapsed, frame / elapsed,
443                                 1e3 * elapsed / frame);
444                 //      chain->print_phase_timing();
445                 }
446
447                 // Reset every 100 frames, so that local variations in frame times
448                 // (especially for the first few frames, when the shaders are
449                 // compiled etc.) don't make it hard to measure for the entire
450                 // remaining duration of the program.
451                 if (frame == 10000) {
452                         frame = 0;
453                         start = now;
454                 }
455                 check_error();
456         }
457 }
458
459 void Mixer::subsample_chroma(GLuint src_tex, GLuint dst_tex)
460 {
461         GLuint vao;
462         glGenVertexArrays(1, &vao);
463         check_error();
464
465         float vertices[] = {
466                 0.0f, 2.0f,
467                 0.0f, 0.0f,
468                 2.0f, 0.0f
469         };
470
471         glBindVertexArray(vao);
472         check_error();
473
474         // Extract Cb/Cr.
475         GLuint fbo = resource_pool->create_fbo(dst_tex);
476         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
477         glViewport(0, 0, WIDTH/2, HEIGHT/2);
478         check_error();
479
480         glUseProgram(cbcr_program_num);
481         check_error();
482
483         glActiveTexture(GL_TEXTURE0);
484         check_error();
485         glBindTexture(GL_TEXTURE_2D, src_tex);
486         check_error();
487         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
488         check_error();
489         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
490         check_error();
491         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
492         check_error();
493
494         float chroma_offset_0[] = { -0.5f / WIDTH, 0.0f };
495         set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
496
497         GLuint position_vbo = fill_vertex_attribute(cbcr_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
498         GLuint texcoord_vbo = fill_vertex_attribute(cbcr_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
499
500         glDrawArrays(GL_TRIANGLES, 0, 3);
501         check_error();
502
503         cleanup_vertex_attribute(cbcr_program_num, "position", position_vbo);
504         cleanup_vertex_attribute(cbcr_program_num, "texcoord", texcoord_vbo);
505
506         glUseProgram(0);
507         check_error();
508
509         resource_pool->release_fbo(fbo);
510         glDeleteVertexArrays(1, &vao);
511 }
512
513 void Mixer::release_display_frame(DisplayFrame *frame)
514 {
515         for (GLuint texnum : frame->temp_textures) {
516                 resource_pool->release_2d_texture(texnum);
517         }
518         frame->temp_textures.clear();
519         frame->ready_fence.reset();
520         frame->input_frames.clear();
521 }
522
523 void Mixer::start()
524 {
525         mixer_thread = std::thread(&Mixer::thread_func, this);
526 }
527
528 void Mixer::quit()
529 {
530         should_quit = true;
531         mixer_thread.join();
532 }
533
534 void Mixer::transition_clicked(int transition_num, float t)
535 {
536         theme->transition_clicked(transition_num, t);
537 }
538
539 void Mixer::OutputChannel::output_frame(DisplayFrame frame)
540 {
541         // Store this frame for display. Remove the ready frame if any
542         // (it was seemingly never used).
543         {
544                 std::unique_lock<std::mutex> lock(frame_mutex);
545                 if (has_ready_frame) {
546                         parent->release_display_frame(&ready_frame);
547                 }
548                 ready_frame = frame;
549                 has_ready_frame = true;
550         }
551
552         if (has_new_frame_ready_callback) {
553                 new_frame_ready_callback();
554         }
555 }
556
557 bool Mixer::OutputChannel::get_display_frame(DisplayFrame *frame)
558 {
559         std::unique_lock<std::mutex> lock(frame_mutex);
560         if (!has_current_frame && !has_ready_frame) {
561                 return false;
562         }
563
564         if (has_current_frame && has_ready_frame) {
565                 // We have a new ready frame. Toss the current one.
566                 parent->release_display_frame(&current_frame);
567                 has_current_frame = false;
568         }
569         if (has_ready_frame) {
570                 assert(!has_current_frame);
571                 current_frame = ready_frame;
572                 ready_frame.ready_fence.reset();  // Drop the refcount.
573                 ready_frame.input_frames.clear();  // Drop the refcounts.
574                 has_current_frame = true;
575                 has_ready_frame = false;
576         }
577
578         *frame = current_frame;
579         return true;
580 }
581
582 void Mixer::OutputChannel::set_frame_ready_callback(Mixer::new_frame_ready_callback_t callback)
583 {
584         new_frame_ready_callback = callback;
585         has_new_frame_ready_callback = true;
586 }