]> 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 878e430106ef2ff8e5542786736fd3d2bac151bb..f77f6d48f7eb0fa4da4bbeb189d40ff24d5a85b6 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -10,6 +10,7 @@
 #include <movit/image_format.h>
 #include <movit/init.h>
 #include <movit/resource_pool.h>
+#include <pthread.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -202,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);
        }
 }
 
@@ -219,7 +221,7 @@ Mixer::~Mixer()
                cards[card_index].capture->stop_dequeue_thread();
                if (cards[card_index].output) {
                        cards[card_index].output->end_output();
-                       delete cards[card_index].output;
+                       cards[card_index].output.reset();
                }
        }
 
@@ -233,11 +235,12 @@ void Mixer::configure_card(unsigned card_index, CaptureInterface *capture, bool
        CaptureCard *card = &cards[card_index];
        if (card->capture != nullptr) {
                card->capture->stop_dequeue_thread();
-               delete card->capture;
        }
-       card->capture = capture;
+       card->capture.reset(capture);
        card->is_fake_capture = is_fake_capture;
-       card->output = output;
+       if (card->output.get() != output) {
+               card->output.reset(output);
+       }
        card->capture->set_frame_callback(bind(&Mixer::bm_frame, this, card_index, _1, _2, _3, _4, _5, _6, _7));
        if (card->frame_allocator == nullptr) {
                card->frame_allocator.reset(new PBOFrameAllocator(8 << 20, global_flags.width, global_flags.height));  // 8 MB.
@@ -258,35 +261,44 @@ 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.
                CaptureCard *old_card = &cards[output_card_index];
                old_card->output->end_output();
 
-               old_card->capture->stop_dequeue_thread();
-               delete old_card->capture;
-
-               old_card->capture = old_card->parked_capture;
+               // Stop the fake card that we put into place.
+               // This needs to _not_ happen under the mutex, to avoid deadlock
+               // (delivering the last frame needs to take the mutex).
+               bmusb::CaptureInterface *fake_capture = old_card->capture.get();
+               lock.unlock();
+               fake_capture->stop_dequeue_thread();
+               lock.lock();
+               old_card->capture = move(old_card->parked_capture);
                old_card->is_fake_capture = false;
-               old_card->parked_capture = nullptr;
                old_card->capture->start_bm_capture();
        }
-
-       CaptureCard *card = &cards[card_index];
-       card->capture->stop_dequeue_thread();
-       card->parked_capture = card->capture;
-       card->capture = nullptr;
-       FakeCapture *capture = new FakeCapture(global_flags.width, global_flags.height, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
-       configure_card(card_index, capture, /*is_fake_capture=*/true, card->output);
-       card->queue_length_policy.reset(card_index);
-       card->capture->start_bm_capture();
-       card->output->start_output(bmdModeHD720p5994, pts_int);  // FIXME
+       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();
+               card->parked_capture = move(card->capture);
+               bmusb::CaptureInterface *fake_capture = new FakeCapture(global_flags.width, global_flags.height, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
+               configure_card(card_index, fake_capture, /*is_fake_capture=*/true, card->output.release());
+               card->queue_length_policy.reset(card_index);
+               card->capture->start_bm_capture();
+               card->output->start_output(bmdModeHD720p5994, pts_int);  // FIXME
+       }
        output_card_index = card_index;
 }
 
@@ -558,6 +570,8 @@ void Mixer::bm_hotplug_remove(unsigned card_index)
 
 void Mixer::thread_func()
 {
+       pthread_setname_np(pthread_self(), "Mixer_OpenGL");
+
        eglBindAPI(EGL_OPENGL_API);
        QOpenGLContext *context = create_context(mixer_surface);
        if (!make_current(context, mixer_surface)) {
@@ -580,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 };
 
@@ -595,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();
@@ -720,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(); });
        }
@@ -828,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);
@@ -849,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();
                }
@@ -925,6 +944,8 @@ void Mixer::render_one_frame(int64_t duration)
 
 void Mixer::audio_thread_func()
 {
+       pthread_setname_np(pthread_self(), "Mixer_Audio");
+
        while (!should_quit) {
                AudioTask task;