+ // Look for a fake capture card where we can stick this in.
+ // Prioritize ones that previously held SRT streams with the
+ // same stream ID, if any exist -- and it multiple exist,
+ // take the one that disconnected the last.
+ int first_free_card_index = -1, last_matching_free_card_index = -1;
+ for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+ CaptureCard *card = &cards[card_index];
+ if (!card->is_fake_capture) {
+ continue;
+ }
+ if (first_free_card_index == -1) {
+ first_free_card_index = card_index;
+ }
+ if (card->last_srt_stream_id == stream_id &&
+ (last_matching_free_card_index == -1 ||
+ card->fake_capture_counter >
+ cards[last_matching_free_card_index].fake_capture_counter)) {
+ last_matching_free_card_index = card_index;
+ }
+ }
+
+ const int free_card_index = (last_matching_free_card_index != -1)
+ ? last_matching_free_card_index : first_free_card_index;