]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Make it possible to choose the DeckLink output video mode through the GUI.
[nageru] / mixer.cpp
index b5311a32ed574354d9ac24b00246431307edff55..f82650ea906948667f242e2b0941e0bba32aaed4 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);
        }
 }
 
@@ -259,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.
@@ -284,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();
@@ -292,7 +297,8 @@ void Mixer::set_output_card(int card_index)
                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
+               desired_output_video_mode = output_video_mode = card->output->pick_video_mode(desired_output_video_mode);
+               card->output->start_output(desired_output_video_mode, pts_int);
        }
        output_card_index = card_index;
 }
@@ -378,7 +384,7 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode,
                } while (!success);
        }
 
-       audio_mixer.add_audio(device, audio_frame.data + audio_offset, num_samples, audio_format, frame_length);
+       audio_mixer.add_audio(device, audio_frame.data + audio_offset, num_samples, audio_format, frame_length, audio_frame.received_timestamp);
 
        // Done with the audio, so release it.
        if (audio_frame.owner) {
@@ -565,6 +571,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)) {
@@ -587,6 +595,17 @@ 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);
+               }
+               if (output_card_index != -1 &&
+                   desired_output_video_mode != output_video_mode) {
+                       DeckLinkOutput *output = cards[output_card_index].output.get();
+                       output->end_output();
+                       desired_output_video_mode = output_video_mode = output->pick_video_mode(desired_output_video_mode);
+                       output->start_output(desired_output_video_mode, pts_int);
+               }
+
                CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS];
                bool has_new_frame[MAX_VIDEO_CARDS] = { false };
 
@@ -602,7 +621,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, output_frame_info.frame_timestamp);
                stats_dropped_frames += output_frame_info.dropped_frames;
 
                handle_hotplugged_cards();
@@ -727,11 +746,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, &output_frame_info.frame_timestamp);
                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(); });
        }
@@ -746,6 +766,11 @@ start:
                goto start;
        }
 
+       if (!master_card_is_output) {
+               output_frame_info.frame_timestamp =
+                       cards[master_card_index].new_frames.front().received_timestamp;
+       }
+
        for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
                CaptureCard *card = &cards[card_index];
                if (card->new_frames.empty()) {
@@ -835,7 +860,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, steady_clock::time_point frame_timestamp)
 {
        // Resample the audio as needed, including from previously dropped frames.
        assert(num_cards > 0);
@@ -856,8 +881,8 @@ 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;
-                       audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame, adjust_rate});
+                       bool adjust_rate = !dropped_frame && !is_preroll;
+                       audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame, adjust_rate, frame_timestamp});
                        audio_task_queue_changed.notify_one();
                }
                if (dropped_frame) {
@@ -932,6 +957,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;
 
@@ -948,7 +975,7 @@ void Mixer::audio_thread_func()
                ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy =
                        task.adjust_rate ? ResamplingQueue::ADJUST_RATE : ResamplingQueue::DO_NOT_ADJUST_RATE;
                vector<float> samples_out = audio_mixer.get_output(
-                       double(task.pts_int) / TIMEBASE,
+                       task.frame_timestamp,
                        task.num_samples,
                        rate_adjustment_policy);
 
@@ -1015,6 +1042,13 @@ void Mixer::start_mode_scanning(unsigned card_index)
        last_mode_scan_change[card_index] = steady_clock::now();
 }
 
+map<uint32_t, bmusb::VideoMode> Mixer::get_available_output_video_modes() const
+{
+       assert(desired_output_card_index != -1);
+       unique_lock<mutex> lock(card_mutex);
+       return cards[desired_output_card_index].output->get_available_video_modes();
+}
+
 Mixer::OutputChannel::~OutputChannel()
 {
        if (has_current_frame) {