]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
More splitting of functions out of thread_func().
[nageru] / mixer.cpp
index 104a6c03a22278b9b9ddfde7e8eb7edc4f9ad96e..5325437e0fddaa56d3044ad206b144340f696eee 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -111,6 +111,29 @@ string generate_local_dump_filename(int frame)
 
 }  // namespace
 
+void QueueLengthPolicy::update_policy(int queue_length)
+{
+       if (queue_length < 0) {  // Starvation.
+               if (safe_queue_length < 5) {
+                       ++safe_queue_length;
+                       fprintf(stderr, "Card %u: Starvation, increasing safe limit to %u frames\n",
+                               card_index, safe_queue_length);
+               }
+               frames_with_at_least_one = 0;
+               return;
+       }
+       if (queue_length > 0) {
+               if (++frames_with_at_least_one >= 50) {
+                       --safe_queue_length;
+                       fprintf(stderr, "Card %u: Spare frames for more than 50 frames, reducing safe limit to %u frames\n",
+                               card_index, safe_queue_length);
+                       frames_with_at_least_one = 0;
+               }
+       } else {
+               frames_with_at_least_one = 0;
+       }
+}
+
 Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        : httpd(WIDTH, HEIGHT),
          num_cards(num_cards),
@@ -182,6 +205,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        }
 
        for (card_index = 0; card_index < num_cards; ++card_index) {
+               cards[card_index].queue_length_policy.reset(card_index);
                cards[card_index].capture->start_bm_capture();
        }
 
@@ -581,31 +605,10 @@ void Mixer::thread_func()
                bool has_new_frame[MAX_CARDS] = { false };
                int num_samples[MAX_CARDS] = { 0 };
 
-               // The first card is the master timer, so wait for it to have a new frame.
                // TODO: Make configurable, and with a timeout.
                unsigned master_card_index = 0;
 
-               {
-                       unique_lock<mutex> lock(bmusb_mutex);
-
-                       cards[master_card_index].new_frames_changed.wait(lock, [this, master_card_index]{ return !cards[master_card_index].new_frames.empty(); });
-
-                       for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
-                               CaptureCard *card = &cards[card_index];
-                               if (card->new_frames.empty()) {
-                                       continue;
-                               }
-                               new_frames[card_index] = move(card->new_frames.front());
-                               has_new_frame[card_index] = true;
-                               card->new_frames.pop();
-                               card->new_frames_changed.notify_all();
-
-                               int num_samples_times_timebase = OUTPUT_FREQUENCY * new_frames[card_index].length + card->fractional_samples;
-                               num_samples[card_index] = num_samples_times_timebase / TIMEBASE;
-                               card->fractional_samples = num_samples_times_timebase % TIMEBASE;
-                               assert(num_samples[card_index] >= 0);
-                       }
-               }
+               get_one_frame_from_each_card(master_card_index, new_frames, has_new_frame, num_samples);
 
                // Resample the audio as needed, including from previously dropped frames.
                assert(num_cards > 0);
@@ -625,18 +628,7 @@ void Mixer::thread_func()
                        }
                }
 
-               if (audio_level_callback != nullptr) {
-                       unique_lock<mutex> lock(compressor_mutex);
-                       double loudness_s = r128.loudness_S();
-                       double loudness_i = r128.integrated();
-                       double loudness_range_low = r128.range_min();
-                       double loudness_range_high = r128.range_max();
-
-                       audio_level_callback(loudness_s, 20.0 * log10(peak),
-                                            loudness_i, loudness_range_low, loudness_range_high,
-                                            gain_staging_db, 20.0 * log10(final_makeup_gain),
-                                            correlation.get_correlation());
-               }
+               send_audio_level_callback();
 
                for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
                        if (card_index == master_card_index || !has_new_frame[card_index]) {
@@ -678,66 +670,10 @@ void Mixer::thread_func()
                        }
                }
 
-               // Get the main chain from the theme, and set its state immediately.
-               Theme::Chain theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT, input_state);
-               EffectChain *chain = theme_main_chain.chain;
-               theme_main_chain.setup_chain();
-               //theme_main_chain.chain->enable_phase_timing(true);
-
-               GLuint y_tex, cbcr_tex;
-               bool got_frame = h264_encoder->begin_frame(&y_tex, &cbcr_tex);
-               assert(got_frame);
-
-               // Render main chain.
-               GLuint cbcr_full_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
-               GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGB565, WIDTH, HEIGHT);  // Saves texture bandwidth, although dithering gets messed up.
-               GLuint fbo = resource_pool->create_fbo(y_tex, cbcr_full_tex, rgba_tex);
-               check_error();
-               chain->render_to_fbo(fbo, WIDTH, HEIGHT);
-               resource_pool->release_fbo(fbo);
-
-               subsample_chroma(cbcr_full_tex, cbcr_tex);
-               resource_pool->release_2d_texture(cbcr_full_tex);
-
-               // Set the right state for rgba_tex.
-               glBindFramebuffer(GL_FRAMEBUFFER, 0);
-               glBindTexture(GL_TEXTURE_2D, rgba_tex);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
-               RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
-               check_error();
-
-               const int64_t av_delay = TIMEBASE / 10;  // Corresponds to the fixed delay in resampling_queue.h. TODO: Make less hard-coded.
-               h264_encoder->end_frame(fence, pts_int + av_delay, theme_main_chain.input_frames);
+               render_one_frame();
                ++frame;
                pts_int += new_frames[master_card_index].length;
 
-               // The live frame just shows the RGBA texture we just rendered.
-               // It owns rgba_tex now.
-               DisplayFrame live_frame;
-               live_frame.chain = display_chain.get();
-               live_frame.setup_chain = [this, rgba_tex]{
-                       display_input->set_texture_num(rgba_tex);
-               };
-               live_frame.ready_fence = fence;
-               live_frame.input_frames = {};
-               live_frame.temp_textures = { rgba_tex };
-               output_channel[OUTPUT_LIVE].output_frame(live_frame);
-
-               // Set up preview and any additional channels.
-               for (int i = 1; i < theme->get_num_channels() + 2; ++i) {
-                       DisplayFrame display_frame;
-                       Theme::Chain chain = theme->get_chain(i, pts(), WIDTH, HEIGHT, input_state);  // FIXME: dimensions
-                       display_frame.chain = chain.chain;
-                       display_frame.setup_chain = chain.setup_chain;
-                       display_frame.ready_fence = fence;
-                       display_frame.input_frames = chain.input_frames;
-                       display_frame.temp_textures = {};
-                       output_channel[i].output_frame(display_frame);
-               }
-
                clock_gettime(CLOCK_MONOTONIC, &now);
                double elapsed = now.tv_sec - start.tv_sec +
                        1e-9 * (now.tv_nsec - start.tv_nsec);
@@ -773,6 +709,119 @@ void Mixer::thread_func()
        resource_pool->clean_context();
 }
 
+void Mixer::get_one_frame_from_each_card(unsigned master_card_index, CaptureCard::NewFrame new_frames[MAX_CARDS], bool has_new_frame[MAX_CARDS], int num_samples[MAX_CARDS])
+{
+       // The first card is the master timer, so wait for it to have a new frame.
+       unique_lock<mutex> lock(bmusb_mutex);
+       cards[master_card_index].new_frames_changed.wait(lock, [this, master_card_index]{ return !cards[master_card_index].new_frames.empty(); });
+
+       for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+               CaptureCard *card = &cards[card_index];
+               if (card->new_frames.empty()) {
+                       assert(card_index != master_card_index);
+                       card->queue_length_policy.update_policy(-1);
+                       continue;
+               }
+               new_frames[card_index] = move(card->new_frames.front());
+               has_new_frame[card_index] = true;
+               card->new_frames.pop();
+               card->new_frames_changed.notify_all();
+
+               int num_samples_times_timebase = OUTPUT_FREQUENCY * new_frames[card_index].length + card->fractional_samples;
+               num_samples[card_index] = num_samples_times_timebase / TIMEBASE;
+               card->fractional_samples = num_samples_times_timebase % TIMEBASE;
+               assert(num_samples[card_index] >= 0);
+
+               if (card_index != master_card_index) {
+                       // If we have excess frames compared to the policy for this card,
+                       // drop frames from the head.
+                       card->queue_length_policy.update_policy(card->new_frames.size());
+                       while (card->new_frames.size() > card->queue_length_policy.get_safe_queue_length()) {
+                               card->new_frames.pop();
+                       }
+               }
+       }
+}
+
+void Mixer::render_one_frame()
+{
+       // Get the main chain from the theme, and set its state immediately.
+       Theme::Chain theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT, input_state);
+       EffectChain *chain = theme_main_chain.chain;
+       theme_main_chain.setup_chain();
+       //theme_main_chain.chain->enable_phase_timing(true);
+
+       GLuint y_tex, cbcr_tex;
+       bool got_frame = h264_encoder->begin_frame(&y_tex, &cbcr_tex);
+       assert(got_frame);
+
+       // Render main chain.
+       GLuint cbcr_full_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
+       GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGB565, WIDTH, HEIGHT);  // Saves texture bandwidth, although dithering gets messed up.
+       GLuint fbo = resource_pool->create_fbo(y_tex, cbcr_full_tex, rgba_tex);
+       check_error();
+       chain->render_to_fbo(fbo, WIDTH, HEIGHT);
+       resource_pool->release_fbo(fbo);
+
+       subsample_chroma(cbcr_full_tex, cbcr_tex);
+       resource_pool->release_2d_texture(cbcr_full_tex);
+
+       // Set the right state for rgba_tex.
+       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+       glBindTexture(GL_TEXTURE_2D, rgba_tex);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+       RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
+       check_error();
+
+       const int64_t av_delay = TIMEBASE / 10;  // Corresponds to the fixed delay in resampling_queue.h. TODO: Make less hard-coded.
+       h264_encoder->end_frame(fence, pts_int + av_delay, theme_main_chain.input_frames);
+
+       // The live frame just shows the RGBA texture we just rendered.
+       // It owns rgba_tex now.
+       DisplayFrame live_frame;
+       live_frame.chain = display_chain.get();
+       live_frame.setup_chain = [this, rgba_tex]{
+               display_input->set_texture_num(rgba_tex);
+       };
+       live_frame.ready_fence = fence;
+       live_frame.input_frames = {};
+       live_frame.temp_textures = { rgba_tex };
+       output_channel[OUTPUT_LIVE].output_frame(live_frame);
+
+       // Set up preview and any additional channels.
+       for (int i = 1; i < theme->get_num_channels() + 2; ++i) {
+               DisplayFrame display_frame;
+               Theme::Chain chain = theme->get_chain(i, pts(), WIDTH, HEIGHT, input_state);  // FIXME: dimensions
+               display_frame.chain = chain.chain;
+               display_frame.setup_chain = chain.setup_chain;
+               display_frame.ready_fence = fence;
+               display_frame.input_frames = chain.input_frames;
+               display_frame.temp_textures = {};
+               output_channel[i].output_frame(display_frame);
+       }
+}
+
+void Mixer::send_audio_level_callback()
+{
+       if (audio_level_callback == nullptr) {
+               return;
+       }
+
+       unique_lock<mutex> lock(compressor_mutex);
+       double loudness_s = r128.loudness_S();
+       double loudness_i = r128.integrated();
+       double loudness_range_low = r128.range_min();
+       double loudness_range_high = r128.range_max();
+
+       audio_level_callback(loudness_s, 20.0 * log10(peak),
+               loudness_i, loudness_range_low, loudness_range_high,
+               gain_staging_db, 20.0 * log10(final_makeup_gain),
+               correlation.get_correlation());
+}
+
 void Mixer::audio_thread_func()
 {
        while (!should_quit) {