10 #include <effect_chain.h>
11 #include <effect_util.h>
12 #include <epoxy/egl.h>
14 #include <image_format.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>
27 #include <white_balance_effect.h>
29 #include <ycbcr_input.h>
31 #include <condition_variable>
41 #include "h264encode.h"
42 #include "pbo_frame_allocator.h"
43 #include "ref_counted_gl_sync.h"
47 using namespace movit;
49 using namespace std::placeholders;
51 Mixer *global_mixer = nullptr;
53 Mixer::Mixer(const QSurfaceFormat &format)
54 : mixer_surface(create_surface(format)),
55 h264_encoder_surface(create_surface(format))
57 CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
60 resource_pool.reset(new ResourcePool);
61 output_channel[OUTPUT_LIVE].parent = this;
62 output_channel[OUTPUT_PREVIEW].parent = this;
63 output_channel[OUTPUT_INPUT0].parent = this;
64 output_channel[OUTPUT_INPUT1].parent = this;
66 ImageFormat inout_format;
67 inout_format.color_space = COLORSPACE_sRGB;
68 inout_format.gamma_curve = GAMMA_sRGB;
70 YCbCrFormat input_ycbcr_format;
71 input_ycbcr_format.chroma_subsampling_x = 2;
72 input_ycbcr_format.chroma_subsampling_y = 1;
73 input_ycbcr_format.cb_x_position = 0.0;
74 input_ycbcr_format.cr_x_position = 0.0;
75 input_ycbcr_format.cb_y_position = 0.5;
76 input_ycbcr_format.cr_y_position = 0.5;
77 input_ycbcr_format.luma_coefficients = YCBCR_REC_601;
78 input_ycbcr_format.full_range = false;
80 YCbCrFormat output_ycbcr_format;
81 output_ycbcr_format.chroma_subsampling_x = 1;
82 output_ycbcr_format.chroma_subsampling_y = 1;
83 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
84 output_ycbcr_format.full_range = false;
87 chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get()));
89 input[0] = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
90 chain->add_input(input[0]);
91 input[1] = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
92 chain->add_input(input[1]);
93 resample_effect = chain->add_effect(new ResampleEffect(), input[0]);
94 padding_effect = chain->add_effect(new IntegralPaddingEffect());
95 float border_color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
96 CHECK(padding_effect->set_vec4("border_color", border_color));
98 resample2_effect = chain->add_effect(new ResampleEffect(), input[1]);
99 Effect *saturation_effect = chain->add_effect(new SaturationEffect());
100 CHECK(saturation_effect->set_float("saturation", 0.3f));
101 Effect *wb_effect = chain->add_effect(new WhiteBalanceEffect());
102 CHECK(wb_effect->set_float("output_color_temperature", 3500.0));
103 padding2_effect = chain->add_effect(new IntegralPaddingEffect());
105 chain->add_effect(new OverlayEffect(), padding_effect, padding2_effect);
107 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
108 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
109 chain->set_dither_bits(8);
110 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
113 // Display chain; shows the live output produced by the main chain (its RGBA version).
114 display_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get()));
116 display_input = new FlatInput(inout_format, FORMAT_RGB, GL_UNSIGNED_BYTE, WIDTH, HEIGHT); // FIXME: GL_UNSIGNED_BYTE is really wrong.
117 display_chain->add_input(display_input);
118 display_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
119 display_chain->set_dither_bits(0); // Don't bother.
120 display_chain->finalize();
122 // Preview chains (always shows just the inputs for now).
123 preview0_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get()));
125 preview0_input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
126 preview0_chain->add_input(preview0_input);
127 preview0_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
128 preview0_chain->set_dither_bits(0); // Don't bother.
129 preview0_chain->finalize();
131 preview1_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get()));
133 preview1_input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
134 preview1_chain->add_input(preview1_input);
135 preview1_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
136 preview1_chain->set_dither_bits(0); // Don't bother.
137 preview1_chain->finalize();
139 h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mp4"));
141 printf("Configuring first card...\n");
142 cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b); // 0xbd4f
143 cards[0].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 0, _1, _2, _3, _4, _5, _6, _7));
144 cards[0].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
145 cards[0].usb->set_video_frame_allocator(cards[0].frame_allocator.get());
146 cards[0].usb->configure_card();
147 cards[0].surface = create_surface(format);
149 cards[1].surface = create_surface(format);
152 if (NUM_CARDS == 2) {
153 printf("Configuring second card...\n");
154 cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
155 cards[1].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 1, _1, _2, _3, _4, _5, _6, _7));
156 cards[1].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
157 cards[1].usb->set_video_frame_allocator(cards[1].frame_allocator.get());
158 cards[1].usb->configure_card();
161 BMUSBCapture::start_bm_thread();
163 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
164 cards[card_index].usb->start_bm_capture();
165 input[card_index]->set_pixel_data(0, nullptr, 0);
166 input[card_index]->set_pixel_data(1, nullptr, 0);
169 //chain->enable_phase_timing(true);
171 // Set up stuff for NV12 conversion.
174 string cbcr_vert_shader = read_file("vs-cbcr.130.vert");
175 string cbcr_frag_shader =
178 "uniform sampler2D cbcr_tex; \n"
180 " gl_FragColor = texture2D(cbcr_tex, tc0); \n"
182 cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
187 resource_pool->release_glsl_program(cbcr_program_num);
188 BMUSBCapture::stop_bm_thread();
191 void Mixer::bm_frame(int card_index, uint16_t timecode,
192 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
193 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
195 CaptureCard *card = &cards[card_index];
196 if (!card->thread_initialized) {
197 printf("initializing context for bmusb thread %d\n", card_index);
198 eglBindAPI(EGL_OPENGL_API);
199 card->context = create_context();
200 if (!make_current(card->context, card->surface)) {
201 printf("failed to create bmusb context\n");
204 card->thread_initialized = true;
207 if (video_frame.len - video_offset != 1280 * 750 * 2) {
208 printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
209 FILE *fp = fopen("frame.raw", "wb");
210 fwrite(video_frame.data, video_frame.len, 1, fp);
213 card->usb->get_video_frame_allocator()->release_frame(video_frame);
214 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
218 // Wait until the previous frame was consumed.
219 std::unique_lock<std::mutex> lock(bmusb_mutex);
220 card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
222 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)video_frame.userdata;
223 GLuint pbo = userdata->pbo;
225 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
227 glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size);
229 //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
232 // Upload the textures.
233 glBindTexture(GL_TEXTURE_2D, userdata->tex_y);
235 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1280, 720, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22));
237 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr);
239 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1280/2, 720, GL_RG, GL_UNSIGNED_BYTE, BUFFER_OFFSET(1280 * 25 + 22));
241 glBindTexture(GL_TEXTURE_2D, 0);
243 GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
245 assert(fence != nullptr);
247 std::unique_lock<std::mutex> lock(bmusb_mutex);
248 card->new_data_ready = true;
249 card->new_frame = RefCountedFrame(video_frame);
250 card->new_data_ready_fence = fence;
251 card->new_data_ready_changed.notify_all();
254 // Video frame will be released when last user of card->new_frame goes out of scope.
255 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
258 void Mixer::place_rectangle(Effect *resample_effect, Effect *padding_effect, float x0, float y0, float x1, float y1)
266 if (x0 > 1280.0 || x1 < 0.0 || y0 > 720.0 || y1 < 0.0) {
267 CHECK(resample_effect->set_int("width", 1));
268 CHECK(resample_effect->set_int("height", 1));
269 CHECK(resample_effect->set_float("zoom_x", 1280.0));
270 CHECK(resample_effect->set_float("zoom_y", 720.0));
271 CHECK(padding_effect->set_int("left", 2000));
272 CHECK(padding_effect->set_int("top", 2000));
276 // Clip. (TODO: Clip on upper/left sides, too.)
278 srcx1 = (1280.0 - x0) / (x1 - x0);
282 srcy1 = (720.0 - y0) / (y1 - y0);
286 float x_subpixel_offset = x0 - floor(x0);
287 float y_subpixel_offset = y0 - floor(y0);
289 // Resampling must be to an integral number of pixels. Round up,
290 // and then add an extra pixel so we have some leeway for the border.
291 int width = int(ceil(x1 - x0)) + 1;
292 int height = int(ceil(y1 - y0)) + 1;
293 CHECK(resample_effect->set_int("width", width));
294 CHECK(resample_effect->set_int("height", height));
296 // Correct the discrepancy with zoom. (This will leave a small
297 // excess edge of pixels and subpixels, which we'll correct for soon.)
298 float zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
299 float zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
300 CHECK(resample_effect->set_float("zoom_x", zoom_x));
301 CHECK(resample_effect->set_float("zoom_y", zoom_y));
302 CHECK(resample_effect->set_float("zoom_center_x", 0.0f));
303 CHECK(resample_effect->set_float("zoom_center_y", 0.0f));
305 // Padding must also be to a whole-pixel offset.
306 CHECK(padding_effect->set_int("left", floor(x0)));
307 CHECK(padding_effect->set_int("top", floor(y0)));
309 // Correct _that_ discrepancy by subpixel offset in the resampling.
310 CHECK(resample_effect->set_float("left", -x_subpixel_offset / zoom_x));
311 CHECK(resample_effect->set_float("top", -y_subpixel_offset / zoom_y));
313 // Finally, adjust the border so it is exactly where we want it.
314 CHECK(padding_effect->set_float("border_offset_left", x_subpixel_offset));
315 CHECK(padding_effect->set_float("border_offset_right", x1 - (floor(x0) + width)));
316 CHECK(padding_effect->set_float("border_offset_top", y_subpixel_offset));
317 CHECK(padding_effect->set_float("border_offset_bottom", y1 - (floor(y0) + height)));
320 void Mixer::thread_func()
322 eglBindAPI(EGL_OPENGL_API);
323 QOpenGLContext *context = create_context();
324 if (!make_current(context, mixer_surface)) {
329 struct timespec start, now;
330 clock_gettime(CLOCK_MONOTONIC, &start);
332 while (!should_quit) {
335 //int width0 = lrintf(848 * (1.0 + 0.2 * sin(frame * 0.02)));
337 int height0 = lrintf(width0 * 9.0 / 16.0);
339 //float top0 = 96 + 48 * sin(frame * 0.005);
340 //float left0 = 96 + 48 * cos(frame * 0.006);
343 float bottom0 = top0 + height0;
344 float right0 = left0 + width0;
349 float bottom1 = 720 - 48;
350 float right1 = 1280 - 16;
351 float top1 = bottom1 - height1;
352 float left1 = right1 - width1;
354 if (current_source == SOURCE_INPUT1) {
361 bottom1 = HEIGHT + 20;
364 } else if (current_source == SOURCE_INPUT2) {
371 bottom0 = HEIGHT + 20;
375 float t = 0.5 + 0.5 * cos(frame * 0.006);
376 float scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
377 float tx0 = 0.0 + t * (-16.0 * scale0);
378 float ty0 = 0.0 + t * (-48.0 * scale0);
380 top0 = top0 * scale0 + ty0;
381 bottom0 = bottom0 * scale0 + ty0;
382 left0 = left0 * scale0 + tx0;
383 right0 = right0 * scale0 + tx0;
385 top1 = top1 * scale0 + ty0;
386 bottom1 = bottom1 * scale0 + ty0;
387 left1 = left1 * scale0 + tx0;
388 right1 = right1 * scale0 + tx0;
391 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
392 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);
394 CaptureCard card_copy[NUM_CARDS];
397 std::unique_lock<std::mutex> lock(bmusb_mutex);
399 // The first card is the master timer, so wait for it to have a new frame.
400 // TODO: Make configurable, and with a timeout.
401 cards[0].new_data_ready_changed.wait(lock, [this]{ return cards[0].new_data_ready; });
403 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
404 CaptureCard *card = &cards[card_index];
405 card_copy[card_index].usb = card->usb;
406 card_copy[card_index].new_data_ready = card->new_data_ready;
407 card_copy[card_index].new_frame = card->new_frame;
408 card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence;
409 card->new_data_ready = false;
410 card->new_data_ready_changed.notify_all();
414 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
415 CaptureCard *card = &card_copy[card_index];
416 if (!card->new_data_ready)
419 bmusb_current_rendering_frame[card_index] = card->new_frame;
422 // The new texture might still be uploaded,
423 // tell the GPU to wait until it's there.
424 if (card->new_data_ready_fence)
425 glWaitSync(card->new_data_ready_fence, /*flags=*/0, GL_TIMEOUT_IGNORED);
427 glDeleteSync(card->new_data_ready_fence);
429 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)card->new_frame->userdata;
430 input[card_index]->set_texture_num(0, userdata->tex_y);
431 input[card_index]->set_texture_num(1, userdata->tex_cbcr);
433 if (NUM_CARDS == 1) {
434 // Set to the other one, too.
435 input[1]->set_texture_num(0, userdata->tex_y);
436 input[1]->set_texture_num(1, userdata->tex_cbcr);
440 GLuint y_tex, cbcr_tex;
441 bool got_frame = h264_encoder->begin_frame(&y_tex, &cbcr_tex);
444 // Render main chain.
445 GLuint cbcr_full_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
446 GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGB565, WIDTH, HEIGHT); // Saves texture bandwidth, although dithering gets messed up.
447 GLuint fbo = resource_pool->create_fbo(y_tex, cbcr_full_tex, rgba_tex);
448 chain->render_to_fbo(fbo, WIDTH, HEIGHT);
449 resource_pool->release_fbo(fbo);
451 subsample_chroma(cbcr_full_tex, cbcr_tex);
452 resource_pool->release_2d_texture(cbcr_full_tex);
454 // Set the right state for rgba_tex.
455 glBindFramebuffer(GL_FRAMEBUFFER, 0);
456 glBindTexture(GL_TEXTURE_2D, rgba_tex);
457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
458 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
459 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
461 RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
464 // Make sure the H.264 gets a reference to all the
465 // input frames needed, so that they are not released back
466 // until the rendering is done.
467 vector<RefCountedFrame> input_frames;
468 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
469 input_frames.push_back(bmusb_current_rendering_frame[card_index]);
471 h264_encoder->end_frame(fence, input_frames);
473 // The live frame just shows the RGBA texture we just rendered.
474 // It owns rgba_tex now.
475 DisplayFrame live_frame;
476 live_frame.chain = display_chain.get();
477 live_frame.setup_chain = [this, rgba_tex]{
478 display_input->set_texture_num(rgba_tex);
480 live_frame.ready_fence = fence;
481 live_frame.input_frames = {};
482 live_frame.temp_textures = { rgba_tex };
483 output_channel[OUTPUT_LIVE].output_frame(live_frame);
485 // The preview frame shows the first input. Note that the textures
486 // are owned by the input frame, not the display frame.
488 const PBOFrameAllocator::Userdata *input0_userdata = (const PBOFrameAllocator::Userdata *)bmusb_current_rendering_frame[0]->userdata;
489 GLuint input0_y_tex = input0_userdata->tex_y;
490 GLuint input0_cbcr_tex = input0_userdata->tex_cbcr;
491 DisplayFrame preview0_frame;
492 preview0_frame.chain = preview0_chain.get();
493 preview0_frame.setup_chain = [this, input0_y_tex, input0_cbcr_tex]{
494 preview0_input->set_texture_num(0, input0_y_tex);
495 preview0_input->set_texture_num(1, input0_cbcr_tex);
497 preview0_frame.ready_fence = fence;
498 preview0_frame.input_frames = { bmusb_current_rendering_frame[0] };
499 preview0_frame.temp_textures = {};
500 output_channel[OUTPUT_PREVIEW].output_frame(preview0_frame);
501 output_channel[OUTPUT_INPUT0].output_frame(preview0_frame);
504 // Same for the other preview.
505 // TODO: Use a for loop. Gah.
507 const PBOFrameAllocator::Userdata *input1_userdata = (const PBOFrameAllocator::Userdata *)bmusb_current_rendering_frame[1]->userdata;
508 GLuint input1_y_tex = input1_userdata->tex_y;
509 GLuint input1_cbcr_tex = input1_userdata->tex_cbcr;
510 DisplayFrame preview1_frame;
511 preview1_frame.chain = preview1_chain.get();
512 preview1_frame.setup_chain = [this, input1_y_tex, input1_cbcr_tex]{
513 preview1_input->set_texture_num(0, input1_y_tex);
514 preview1_input->set_texture_num(1, input1_cbcr_tex);
516 preview1_frame.ready_fence = fence;
517 preview1_frame.input_frames = { bmusb_current_rendering_frame[1] };
518 preview1_frame.temp_textures = {};
519 output_channel[OUTPUT_INPUT1].output_frame(preview1_frame);
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 if (frame % 100 == 0) {
526 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
527 frame, elapsed, frame / elapsed,
528 1e3 * elapsed / frame);
529 // chain->print_phase_timing();
532 // Reset every 100 frames, so that local variations in frame times
533 // (especially for the first few frames, when the shaders are
534 // compiled etc.) don't make it hard to measure for the entire
535 // remaining duration of the program.
536 if (frame == 10000) {
544 void Mixer::subsample_chroma(GLuint src_tex, GLuint dst_tex)
547 glGenVertexArrays(1, &vao);
556 glBindVertexArray(vao);
560 GLuint fbo = resource_pool->create_fbo(dst_tex);
561 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
562 glViewport(0, 0, WIDTH/2, HEIGHT/2);
565 glUseProgram(cbcr_program_num);
568 glActiveTexture(GL_TEXTURE0);
570 glBindTexture(GL_TEXTURE_2D, src_tex);
572 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
574 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
576 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
579 float chroma_offset_0[] = { -0.5f / WIDTH, 0.0f };
580 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
582 GLuint position_vbo = fill_vertex_attribute(cbcr_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
583 GLuint texcoord_vbo = fill_vertex_attribute(cbcr_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices.
585 glDrawArrays(GL_TRIANGLES, 0, 3);
588 cleanup_vertex_attribute(cbcr_program_num, "position", position_vbo);
589 cleanup_vertex_attribute(cbcr_program_num, "texcoord", texcoord_vbo);
594 resource_pool->release_fbo(fbo);
595 glDeleteVertexArrays(1, &vao);
598 void Mixer::release_display_frame(DisplayFrame *frame)
600 for (GLuint texnum : frame->temp_textures) {
601 resource_pool->release_2d_texture(texnum);
603 frame->temp_textures.clear();
604 frame->ready_fence.reset();
605 frame->input_frames.clear();
610 mixer_thread = std::thread(&Mixer::thread_func, this);
619 void Mixer::cut(Source source)
621 current_source = source;
624 void Mixer::OutputChannel::output_frame(DisplayFrame frame)
626 // Store this frame for display. Remove the ready frame if any
627 // (it was seemingly never used).
629 std::unique_lock<std::mutex> lock(frame_mutex);
630 if (has_ready_frame) {
631 parent->release_display_frame(&ready_frame);
634 has_ready_frame = true;
637 if (has_new_frame_ready_callback) {
638 new_frame_ready_callback();
642 bool Mixer::OutputChannel::get_display_frame(DisplayFrame *frame)
644 std::unique_lock<std::mutex> lock(frame_mutex);
645 if (!has_current_frame && !has_ready_frame) {
649 if (has_current_frame && has_ready_frame) {
650 // We have a new ready frame. Toss the current one.
651 parent->release_display_frame(¤t_frame);
652 has_current_frame = false;
654 if (has_ready_frame) {
655 assert(!has_current_frame);
656 current_frame = ready_frame;
657 ready_frame.ready_fence.reset(); // Drop the refcount.
658 ready_frame.input_frames.clear(); // Drop the refcounts.
659 has_current_frame = true;
660 has_ready_frame = false;
663 *frame = current_frame;
667 void Mixer::OutputChannel::set_frame_ready_callback(Mixer::new_frame_ready_callback_t callback)
669 new_frame_ready_callback = callback;
670 has_new_frame_ready_callback = true;