From 134f9d70a4cd03dc1105d9d94b6003fedcef4760 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 29 Jan 2017 01:08:06 +0100 Subject: [PATCH] Do not try to adjust the audio timer during HDMI/SDI output preroll. --- decklink_output.cpp | 5 ++++- decklink_output.h | 2 +- mixer.cpp | 9 +++++---- mixer.h | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/decklink_output.cpp b/decklink_output.cpp index b3c4c85..a826dfe 100644 --- a/decklink_output.cpp +++ b/decklink_output.cpp @@ -242,7 +242,7 @@ void DeckLinkOutput::send_audio(int64_t pts, const std::vector &samples) } } -void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration) +void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll) { assert(!should_quit); @@ -255,9 +255,12 @@ void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *f // While prerolling, we send out frames as quickly as we can. if (target_time < base_pts) { + *is_preroll = true; return; } + *is_preroll = !playback_started; + if (!playback_started) { if (output->EndAudioPreroll() != S_OK) { fprintf(stderr, "Could not end audio preroll\n"); diff --git a/decklink_output.h b/decklink_output.h index 122c8fe..7bc9850 100644 --- a/decklink_output.h +++ b/decklink_output.h @@ -41,7 +41,7 @@ public: void send_frame(GLuint y_tex, GLuint cbcr_tex, const std::vector &input_frames, int64_t pts, int64_t duration); void send_audio(int64_t pts, const std::vector &samples); - void wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration); + void wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll); // Analogous to CaptureInterface. Will only return modes that have the right width/height. std::map get_available_video_modes() const { return video_modes; } diff --git a/mixer.cpp b/mixer.cpp index 0a65727..f77f6d4 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -613,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(); @@ -738,11 +738,12 @@ start: unique_lock 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(); }); } @@ -846,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); @@ -867,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 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(); } diff --git a/mixer.h b/mixer.h index 9e4c4d5..045e9b1 100644 --- a/mixer.h +++ b/mixer.h @@ -323,7 +323,7 @@ private: void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1); void thread_func(); void handle_hotplugged_cards(); - void schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame); + void schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame, bool is_preroll); void render_one_frame(int64_t duration); void audio_thread_func(); void release_display_frame(DisplayFrame *frame); @@ -402,6 +402,7 @@ private: int dropped_frames; // Since last frame. int num_samples; // Audio samples needed for this output frame. int64_t frame_duration; // In TIMEBASE units. + bool is_preroll; }; OutputFrameInfo get_one_frame_from_each_card(unsigned master_card_index, bool master_card_is_output, CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS], bool has_new_frame[MAX_VIDEO_CARDS]); -- 2.39.2