]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Do not try to adjust the audio timer during HDMI/SDI output preroll.
[nageru] / mixer.cpp
index 457734d29578d80696cb2dfdb55f22b669a5e625..f77f6d48f7eb0fa4da4bbeb189d40ff24d5a85b6 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -203,7 +203,8 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
                alsa.reset(new ALSAOutput(OUTPUT_FREQUENCY, /*num_channels=*/2));
        }
        if (global_flags.output_card != -1) {
-               set_output_card(global_flags.output_card);
+               desired_output_card_index = global_flags.output_card;
+               set_output_card_internal(global_flags.output_card);
        }
 }
 
@@ -260,11 +261,11 @@ void Mixer::configure_card(unsigned card_index, CaptureInterface *capture, bool
        audio_mixer.trigger_state_changed_callback();
 }
 
-void Mixer::set_output_card(int card_index)
+void Mixer::set_output_card_internal(int card_index)
 {
-       if (card_index == output_card_index) {
-               return;
-       }
+       // We don't really need to take card_mutex, since we're in the mixer
+       // thread and don't mess with any queues (which is the only thing that happens
+       // from other threads), but it's probably the safest in the long run.
        unique_lock<mutex> lock(card_mutex);
        if (output_card_index != -1) {
                // Switch the old card from output to input.
@@ -285,6 +286,9 @@ void Mixer::set_output_card(int card_index)
        if (card_index != -1) {
                CaptureCard *card = &cards[card_index];
                bmusb::CaptureInterface *capture = card->capture.get();
+               // TODO: DeckLinkCapture::stop_dequeue_thread can actually take
+               // several seconds to complete (blocking on DisableVideoInput);
+               // see if we can maybe do it asynchronously.
                lock.unlock();
                capture->stop_dequeue_thread();
                lock.lock();
@@ -590,6 +594,10 @@ void Mixer::thread_func()
        int stats_dropped_frames = 0;
 
        while (!should_quit) {
+               if (desired_output_card_index != output_card_index) {
+                       set_output_card_internal(desired_output_card_index);
+               }
+
                CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS];
                bool has_new_frame[MAX_VIDEO_CARDS] = { false };
 
@@ -605,7 +613,7 @@ void Mixer::thread_func()
                }
 
                OutputFrameInfo output_frame_info = get_one_frame_from_each_card(master_card_index, master_card_is_output, new_frames, has_new_frame);
-               schedule_audio_resampling_tasks(output_frame_info.dropped_frames, output_frame_info.num_samples, output_frame_info.frame_duration);
+               schedule_audio_resampling_tasks(output_frame_info.dropped_frames, output_frame_info.num_samples, output_frame_info.frame_duration, output_frame_info.is_preroll);
                stats_dropped_frames += output_frame_info.dropped_frames;
 
                handle_hotplugged_cards();
@@ -730,11 +738,12 @@ start:
        unique_lock<mutex> lock(card_mutex, defer_lock);
        if (master_card_is_output) {
                // Clocked to the output, so wait for it to be ready for the next frame.
-               cards[master_card_index].output->wait_for_frame(pts_int, &output_frame_info.dropped_frames, &output_frame_info.frame_duration);
+               cards[master_card_index].output->wait_for_frame(pts_int, &output_frame_info.dropped_frames, &output_frame_info.frame_duration, &output_frame_info.is_preroll);
                lock.lock();
        } else {
                // Wait for the master card to have a new frame.
                // TODO: Add a timeout.
+               output_frame_info.is_preroll = false;
                lock.lock();
                cards[master_card_index].new_frames_changed.wait(lock, [this, master_card_index]{ return !cards[master_card_index].new_frames.empty() || cards[master_card_index].capture->get_disconnected(); });
        }
@@ -838,7 +847,7 @@ void Mixer::handle_hotplugged_cards()
 }
 
 
-void Mixer::schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame)
+void Mixer::schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame, bool is_preroll)
 {
        // Resample the audio as needed, including from previously dropped frames.
        assert(num_cards > 0);
@@ -859,7 +868,7 @@ void Mixer::schedule_audio_resampling_tasks(unsigned dropped_frames, int num_sam
                        // since dropped frames are expected to be rare, and it might be
                        // better to just wait until we have a slightly more normal situation).
                        unique_lock<mutex> lock(audio_mutex);
-                       bool adjust_rate = !dropped_frame;
+                       bool adjust_rate = !dropped_frame && !is_preroll;
                        audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame, adjust_rate});
                        audio_task_queue_changed.notify_one();
                }