From 2e5386ddae01a50ff36a6cda1e73a4c180f6e8d4 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 1 Jun 2020 21:38:16 +0200 Subject: [PATCH] When hot-unplugging capture cards, actually allow making them inactive. This was an oversight; removed cards would always be replaced by fake ones. However, this exposed a few tricky issues, like that the master card can go away, that needed to be dealt with. --- nageru/mixer.cpp | 38 ++++++++++++++++++++++++++------------ nageru/theme.cpp | 12 ++++++++++++ nageru/theme.h | 5 +++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/nageru/mixer.cpp b/nageru/mixer.cpp index cbca327..5df80df 100644 --- a/nageru/mixer.cpp +++ b/nageru/mixer.cpp @@ -1276,6 +1276,11 @@ void Mixer::thread_func() output->start_output(desired_output_video_mode, pts_int); } + { + lock_guard lock(card_mutex); + handle_hotplugged_cards(); + } + CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS]; bool has_new_frame[MAX_VIDEO_CARDS] = { false }; @@ -1290,11 +1295,6 @@ void Mixer::thread_func() assert(master_card_index < MAX_VIDEO_CARDS); } - { - lock_guard lock(card_mutex); - handle_hotplugged_cards(); - } - vector raw_audio[MAX_VIDEO_CARDS]; // For MJPEG encoding. OutputFrameInfo output_frame_info = get_one_frame_from_each_card(master_card_index, master_card_is_output, new_frames, has_new_frame, raw_audio); 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); @@ -1485,8 +1485,9 @@ start: lock.lock(); timed_out = !cards[master_card_index].new_frames_changed.wait_for(lock, master_card_timeout, - [this, master_card_index]{ + [this, master_card_index] { return !cards[master_card_index].new_frames.empty() || + cards[master_card_index].capture == nullptr || cards[master_card_index].capture->get_disconnected(); }); if (timed_out) { @@ -1507,7 +1508,8 @@ start: } else if (cards[master_card_index].new_frames.empty()) { // We were woken up, but not due to a new frame. Deal with it // and then restart. - assert(cards[master_card_index].capture->get_disconnected()); + assert(cards[master_card_index].capture == nullptr || + cards[master_card_index].capture->get_disconnected()); handle_hotplugged_cards(); lock.unlock(); goto start; @@ -1594,11 +1596,23 @@ void Mixer::handle_hotplugged_cards() for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) { CaptureCard *card = &cards[card_index]; if (card->capture != nullptr && card->capture->get_disconnected()) { - fprintf(stderr, "Card %u went away, replacing with a fake card.\n", card_index); - 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, CardType::FAKE_CAPTURE, /*output=*/nullptr, /*is_srt_card=*/false); - card->queue_length_policy.reset(card_index); - card->capture->start_bm_capture(); + bool is_active = card_index < unsigned(global_flags.min_num_cards) || cards[card_index].force_active; + if (is_active) { + fprintf(stderr, "Card %u went away, replacing with a fake card.\n", card_index); + 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, CardType::FAKE_CAPTURE, /*output=*/nullptr, /*is_srt_card=*/false); + card->queue_length_policy.reset(card_index); + card->capture->start_bm_capture(); + } else { + // NOTE: The theme might end up forcing the card back at some later point + // (ie., force_active is false now, but might immediately be true again on + // e.g. the next frame). That should be rare, though, so we don't bother + // adjusting the message. + fprintf(stderr, "Card %u went away, removing. (To keep a fake card, increase --num-cards.)\n", card_index); + theme->remove_card(card_index); + configure_card(card_index, /*capture=*/nullptr, CardType::FAKE_CAPTURE, /*output=*/nullptr, /*is_srt_card=*/false); + card->queue_length_policy.reset(card_index); + } } } diff --git a/nageru/theme.cpp b/nageru/theme.cpp index bb7613c..4f7b143 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -2128,3 +2128,15 @@ string Theme::format_status_line(const string &disk_space_left_text, double file assert(lua_gettop(L) == 0); return text; } + +void Theme::remove_card(unsigned card_index) +{ + lock_guard lock(map_m); + for (auto it = signal_to_card_mapping.begin(); it != signal_to_card_mapping.end(); ) { + if (it->second == int(card_index)) { + it = signal_to_card_mapping.erase(it); + } else { + ++it; + } + } +} diff --git a/nageru/theme.h b/nageru/theme.h index ce232fc..024df7a 100644 --- a/nageru/theme.h +++ b/nageru/theme.h @@ -192,6 +192,11 @@ public: std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds); + // Signal that the given card is going away and will not be replaced + // with a fake capture card, so remove all connections to it so that + // they don't automatically come back on the next frame. + void remove_card(unsigned card_index); + private: void register_globals(); void register_class(const char *class_name, const luaL_Reg *funcs, EffectType effect_type = NO_EFFECT_TYPE); -- 2.39.2