From 9a39f2454113c28963d64aef9722837d9e89150d Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 18 Sep 2024 17:50:28 +0200 Subject: [PATCH] Fix frame selection for multiple encoders --- client/decoder/android/android_decoder.cpp | 2 +- client/scenes/stream.cpp | 39 ++++++++++++++++------ client/scenes/stream.h | 3 +- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/client/decoder/android/android_decoder.cpp b/client/decoder/android/android_decoder.cpp index cb2b5c25..fcc9f696 100644 --- a/client/decoder/android/android_decoder.cpp +++ b/client/decoder/android/android_decoder.cpp @@ -95,7 +95,7 @@ decoder::decoder( description.video_height, AIMAGE_FORMAT_PRIVATE, AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, - 5 /* maxImages */, + scenes::stream::image_buffer_size + 2 /* maxImages */, &ir), "AImageReader_newWithUsage"); image_reader.reset(ir, AImageReader_deleter{}); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index f1c2b31d..2b863245 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -286,9 +286,7 @@ std::vector> scenes::stream::com return {}; std::unique_lock lock(frames_mutex); thread_local std::vector common_frames; - thread_local std::vector tmp; common_frames.clear(); - auto proj = [](const shard_accumulator::blit_handle * h) { return h ? h->feedback.frame_index : -1; }; for (size_t i = 0; i < decoders.size(); ++i) { if (i == 0) @@ -299,15 +297,18 @@ std::vector> scenes::stream::com } else { - tmp.clear(); - std::ranges::set_intersection( - std::ranges::views::transform(decoders[i].latest_frames, [](auto ptr) { return ptr.get(); }), - common_frames, - std::back_inserter(tmp), - std::ranges::less{}, - proj, - proj); - std::swap(common_frames, tmp); + // clang-format off + std::erase_if(common_frames, + [this, i](auto & left) + { + return std::ranges::none_of( + decoders[i].latest_frames, + [&left](auto & right) + { + return left->feedback.frame_index == right->feedback.frame_index; + }); + }); + // clang-format on } } std::optional frame_index; @@ -324,6 +325,22 @@ std::vector> scenes::stream::com assert(*min); frame_index = (*min)->feedback.frame_index; } + else + { + spdlog::warn("Failed to find a common frame for all decoders, dumping available frames per decoder"); + for (const auto & decoder: decoders) + { + std::string frames; + for (const auto & frame: decoder.latest_frames) + { + if (frame) + frames += " " + std::to_string(frame->feedback.frame_index); + else + frames += " -"; + } + spdlog::warn(frames); + } + } std::vector> result; result.reserve(decoders.size()); for (const auto & decoder: decoders) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index ac7e6732..e271587b 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -43,6 +43,7 @@ public: streaming, stalled }; + static const size_t image_buffer_size = 3; private: static const size_t view_count = 2; @@ -57,7 +58,7 @@ private: vk::raii::PipelineLayout blit_pipeline_layout = nullptr; vk::raii::Pipeline blit_pipeline = nullptr; // latest frames from oldest to most recent - std::array, 3> latest_frames; + std::array, image_buffer_size> latest_frames; std::shared_ptr frame(std::optional id) const; std::vector frames() const;