diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 8872b13dc..bf1bf6a6c 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -1200,11 +1200,6 @@ namespace nvhttp { return false; } - /** - * @brief Get codec mode flags. - * - * @return Moonlight codec capability bitmask for the currently probed encoders. - */ uint32_t get_codec_mode_flags() { uint32_t codec_mode_flags = SCM_H264; if (video::last_encoder_probe_supported_yuv444_for_codec[0]) { @@ -1216,10 +1211,10 @@ namespace nvhttp { codec_mode_flags |= SCM_HEVC_REXT8_444; } } - if (video::active_hevc_mode == 3 || video::active_hevc_mode == 5) { + if ((video::active_hevc_mode == 3 || video::active_hevc_mode == 5) && video::last_encoder_probe_supported_dynamic_range_for_codec[1]) { codec_mode_flags |= SCM_HEVC_MAIN10; } - if ((video::active_hevc_mode == 4 || video::active_hevc_mode == 5) && video::last_encoder_probe_supported_yuv444_for_codec[1]) { + if ((video::active_hevc_mode == 4 || video::active_hevc_mode == 5) && video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[1]) { codec_mode_flags |= SCM_HEVC_REXT10_444; } @@ -1229,10 +1224,10 @@ namespace nvhttp { codec_mode_flags |= SCM_AV1_HIGH8_444; } } - if (video::active_av1_mode == 3 || video::active_av1_mode == 5) { + if ((video::active_av1_mode == 3 || video::active_av1_mode == 5) && video::last_encoder_probe_supported_dynamic_range_for_codec[2]) { codec_mode_flags |= SCM_AV1_MAIN10; } - if ((video::active_av1_mode == 4 || video::active_av1_mode == 5) && video::last_encoder_probe_supported_yuv444_for_codec[2]) { + if ((video::active_av1_mode == 4 || video::active_av1_mode == 5) && video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[2]) { codec_mode_flags |= SCM_AV1_HIGH10_444; } return codec_mode_flags; diff --git a/src/nvhttp.h b/src/nvhttp.h index 2b31a35ff..a7c0fc9ad 100644 --- a/src/nvhttp.h +++ b/src/nvhttp.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -379,6 +380,13 @@ namespace nvhttp { */ void erase_all_clients(); + /** + * @brief Get codec mode flags. + * + * @return Moonlight codec capability bitmask for the currently probed encoders. + */ + uint32_t get_codec_mode_flags(); + #ifdef SUNSHINE_TESTS /** * @brief Test-only accessors for paired-client authorization state. diff --git a/src/video.cpp b/src/video.cpp index 82a1a4adc..13ece1721 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -1473,6 +1473,8 @@ namespace video { int active_av1_mode; ///< AV1 mode selected by the most recent encoder probe. bool last_encoder_probe_supported_ref_frames_invalidation = false; ///< Whether the last probe found reference-frame invalidation support. std::array last_encoder_probe_supported_yuv444_for_codec = {}; ///< YUV444 support discovered for each probed codec. + std::array last_encoder_probe_supported_dynamic_range_for_codec = {}; ///< 10-bit 4:2:0 support discovered for each probed codec. + std::array last_encoder_probe_supported_dynamic_range_yuv444_for_codec = {}; ///< 10-bit YUV444 support discovered for each probed codec. /** * @brief Recreate a display capture object after a capture failure. @@ -3261,6 +3263,35 @@ namespace video { return true; } + void reset_encoder_probe_results() { + last_encoder_probe_supported_ref_frames_invalidation = false; + last_encoder_probe_supported_yuv444_for_codec = {}; + last_encoder_probe_supported_dynamic_range_for_codec = {}; + last_encoder_probe_supported_dynamic_range_yuv444_for_codec = {}; + } + + void record_encoder_probe_results(const encoder_t &encoder) { + last_encoder_probe_supported_ref_frames_invalidation = (encoder.flags & REF_FRAMES_INVALIDATION); + last_encoder_probe_supported_yuv444_for_codec[0] = encoder.h264[encoder_t::PASSED] && + encoder.h264[encoder_t::YUV444]; + last_encoder_probe_supported_yuv444_for_codec[1] = encoder.hevc[encoder_t::PASSED] && + encoder.hevc[encoder_t::YUV444]; + last_encoder_probe_supported_yuv444_for_codec[2] = encoder.av1[encoder_t::PASSED] && + encoder.av1[encoder_t::YUV444]; + last_encoder_probe_supported_dynamic_range_for_codec[0] = encoder.h264[encoder_t::PASSED] && + encoder.h264[encoder_t::DYNAMIC_RANGE]; + last_encoder_probe_supported_dynamic_range_for_codec[1] = encoder.hevc[encoder_t::PASSED] && + encoder.hevc[encoder_t::DYNAMIC_RANGE]; + last_encoder_probe_supported_dynamic_range_for_codec[2] = encoder.av1[encoder_t::PASSED] && + encoder.av1[encoder_t::DYNAMIC_RANGE]; + last_encoder_probe_supported_dynamic_range_yuv444_for_codec[0] = encoder.h264[encoder_t::PASSED] && + encoder.h264[encoder_t::DYNAMIC_RANGE_YUV444]; + last_encoder_probe_supported_dynamic_range_yuv444_for_codec[1] = encoder.hevc[encoder_t::PASSED] && + encoder.hevc[encoder_t::DYNAMIC_RANGE_YUV444]; + last_encoder_probe_supported_dynamic_range_yuv444_for_codec[2] = encoder.av1[encoder_t::PASSED] && + encoder.av1[encoder_t::DYNAMIC_RANGE_YUV444]; + } + int probe_encoders() { if (!allow_encoder_probing()) { // Error already logged @@ -3279,7 +3310,7 @@ namespace video { chosen_encoder = nullptr; active_hevc_mode = config::video.hevc_mode; active_av1_mode = config::video.av1_mode; - last_encoder_probe_supported_ref_frames_invalidation = false; + reset_encoder_probe_results(); auto adjust_encoder_constraints_hevc = [&](encoder_t *encoder) { // If we can't satisfy both the encoder and codec requirement, prefer the encoder over codec support @@ -3429,13 +3460,7 @@ namespace video { auto &encoder = *chosen_encoder; - last_encoder_probe_supported_ref_frames_invalidation = (encoder.flags & REF_FRAMES_INVALIDATION); - last_encoder_probe_supported_yuv444_for_codec[0] = encoder.h264[encoder_t::PASSED] && - encoder.h264[encoder_t::YUV444]; - last_encoder_probe_supported_yuv444_for_codec[1] = encoder.hevc[encoder_t::PASSED] && - encoder.hevc[encoder_t::YUV444]; - last_encoder_probe_supported_yuv444_for_codec[2] = encoder.av1[encoder_t::PASSED] && - encoder.av1[encoder_t::YUV444]; + record_encoder_probe_results(encoder); BOOST_LOG(debug) << "------ h264 ------"sv; for (int x = 0; x < encoder_t::MAX_FLAGS; ++x) { diff --git a/src/video.h b/src/video.h index a6073b0c4..2092fcacf 100644 --- a/src/video.h +++ b/src/video.h @@ -688,6 +688,8 @@ namespace video { extern int active_av1_mode; extern bool last_encoder_probe_supported_ref_frames_invalidation; extern std::array last_encoder_probe_supported_yuv444_for_codec; // 0 - H.264, 1 - HEVC, 2 - AV1 + extern std::array last_encoder_probe_supported_dynamic_range_for_codec; // 0 - H.264, 1 - HEVC, 2 - AV1 + extern std::array last_encoder_probe_supported_dynamic_range_yuv444_for_codec; // 0 - H.264, 1 - HEVC, 2 - AV1 /** * @brief Resolve a client-requested dynamic range against probed encoder capabilities. @@ -698,6 +700,19 @@ namespace video { */ config_t resolve_dynamic_range(const encoder_t &encoder, config_t config); + /** + * @brief Clear all published encoder probe results. + */ + void reset_encoder_probe_results(); + + /** + * @brief Publish the capabilities of a probed encoder for client advertisement. + * @details A codec that did not pass its probe publishes no capabilities. + * + * @param encoder Encoder whose probed codec capabilities should be published. + */ + void record_encoder_probe_results(const encoder_t &encoder); + /** * @brief Capture and encode video for a streaming session. * diff --git a/tests/unit/test_nvhttp_codec_flags.cpp b/tests/unit/test_nvhttp_codec_flags.cpp new file mode 100644 index 000000000..2c6336ebf --- /dev/null +++ b/tests/unit/test_nvhttp_codec_flags.cpp @@ -0,0 +1,148 @@ +/** + * @file tests/unit/test_nvhttp_codec_flags.cpp + * @brief Test the codec capability bitmask advertised to clients. + */ + +// test includes +#include "../tests_common.h" + +// standard includes +#include +#include +#include + +// local includes +#include +#include +#include + +namespace { + /** + * @brief Codec under test, indexed like the `video::last_encoder_probe_*` arrays. + */ + enum class codec_e { + hevc = 1, ///< HEVC. + av1 = 2, ///< AV1. + }; + + /** + * @brief Probe results and configured mode for one codec. + */ + struct flags_case_t { + codec_e codec; ///< Codec under test. + int mode; ///< Configured `hevc_mode` or `av1_mode`. + bool yuv444; ///< 8-bit 4:4:4 probe result. + bool dynamic_range; ///< 10-bit 4:2:0 probe result. + bool dynamic_range_yuv444; ///< 10-bit 4:4:4 probe result. + std::uint32_t expected; ///< Expected flags within the codec's mask. + }; + + constexpr std::uint32_t HEVC_MASK = SCM_HEVC | SCM_HEVC_MAIN10 | SCM_HEVC_REXT8_444 | SCM_HEVC_REXT10_444; + constexpr std::uint32_t AV1_MASK = SCM_AV1_MAIN8 | SCM_AV1_MAIN10 | SCM_AV1_HIGH8_444 | SCM_AV1_HIGH10_444; +} // namespace + +/** + * @brief Save and restore the global probe state so no encoder hardware is needed. + */ +class CodecProbeStateTest: public testing::Test { +protected: + /** + * @brief Save the global probe state. + */ + void SetUp() override { + hevc_mode_ = video::active_hevc_mode; + av1_mode_ = video::active_av1_mode; + yuv444_ = video::last_encoder_probe_supported_yuv444_for_codec; + dynamic_range_ = video::last_encoder_probe_supported_dynamic_range_for_codec; + dynamic_range_yuv444_ = video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec; + } + + /** + * @brief Restore the global probe state. + */ + void TearDown() override { + video::active_hevc_mode = hevc_mode_; + video::active_av1_mode = av1_mode_; + video::last_encoder_probe_supported_yuv444_for_codec = yuv444_; + video::last_encoder_probe_supported_dynamic_range_for_codec = dynamic_range_; + video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec = dynamic_range_yuv444_; + } + +private: + int hevc_mode_ = 0; ///< Saved HEVC mode. + int av1_mode_ = 0; ///< Saved AV1 mode. + std::array yuv444_ {}; ///< Saved 8-bit 4:4:4 results. + std::array dynamic_range_ {}; ///< Saved 10-bit 4:2:0 results. + std::array dynamic_range_yuv444_ {}; ///< Saved 10-bit 4:4:4 results. +}; + +/** + * @brief Parameterized probe results and modes applied directly to the global state. + */ +class CodecModeFlagsTest: public CodecProbeStateTest, public testing::WithParamInterface {}; + +/** + * @brief End-to-end probe-to-bitmask coverage using a hand-built encoder. + */ +using CodecProbeAdvertisementTest = CodecProbeStateTest; + +TEST_F(CodecProbeAdvertisementTest, Probed8Bit444Without10Bit444AdvertisesOnly8Bit444) { + using namespace std::literals; + + video::encoder_t encoder {"test"sv, {}, {}, {}, {}, 0}; + for (auto *codec : {&encoder.h264, &encoder.hevc, &encoder.av1}) { + (*codec)[video::encoder_t::PASSED] = true; + (*codec)[video::encoder_t::YUV444] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE_YUV444] = false; + } + + video::active_hevc_mode = 5; + video::active_av1_mode = 5; + video::reset_encoder_probe_results(); + video::record_encoder_probe_results(encoder); + + constexpr std::uint32_t expected = SCM_H264 | SCM_H264_HIGH8_444 | + SCM_HEVC | SCM_HEVC_MAIN10 | SCM_HEVC_REXT8_444 | + SCM_AV1_MAIN8 | SCM_AV1_MAIN10 | SCM_AV1_HIGH8_444; + const auto flags = nvhttp::get_codec_mode_flags(); + EXPECT_EQ(expected, flags); + EXPECT_EQ(0u, flags & (SCM_HEVC_REXT10_444 | SCM_AV1_HIGH10_444)); +} + +TEST_P(CodecModeFlagsTest, AdvertisesOnlyProbedAndConfiguredModes) { + using enum codec_e; + const auto &c = GetParam(); + const auto index = static_cast(std::to_underlying(c.codec)); + + video::active_hevc_mode = c.codec == hevc ? c.mode : 1; + video::active_av1_mode = c.codec == av1 ? c.mode : 1; + video::last_encoder_probe_supported_yuv444_for_codec = {}; + video::last_encoder_probe_supported_dynamic_range_for_codec = {}; + video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec = {}; + video::last_encoder_probe_supported_yuv444_for_codec[index] = c.yuv444; + video::last_encoder_probe_supported_dynamic_range_for_codec[index] = c.dynamic_range; + video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[index] = c.dynamic_range_yuv444; + + const auto mask = c.codec == hevc ? HEVC_MASK : AV1_MASK; + EXPECT_EQ(c.expected, nvhttp::get_codec_mode_flags() & mask); +} + +INSTANTIATE_TEST_SUITE_P( + CodecModeFlagsTests, + CodecModeFlagsTest, + testing::Values( + // 8-bit 4:4:4 passes but 10-bit 4:4:4 fails. + flags_case_t {codec_e::hevc, 5, true, true, false, SCM_HEVC | SCM_HEVC_MAIN10 | SCM_HEVC_REXT8_444}, + flags_case_t {codec_e::av1, 5, true, true, false, SCM_AV1_MAIN8 | SCM_AV1_MAIN10 | SCM_AV1_HIGH8_444}, + // 10-bit 4:4:4 passes but 10-bit 4:2:0 fails under mode 5. + flags_case_t {codec_e::hevc, 5, true, false, true, SCM_HEVC | SCM_HEVC_REXT8_444 | SCM_HEVC_REXT10_444}, + flags_case_t {codec_e::av1, 5, true, false, true, SCM_AV1_MAIN8 | SCM_AV1_HIGH8_444 | SCM_AV1_HIGH10_444}, + // 10-bit 4:4:4 passes but 8-bit 4:4:4 fails. + flags_case_t {codec_e::hevc, 4, false, true, true, SCM_HEVC | SCM_HEVC_REXT10_444}, + flags_case_t {codec_e::av1, 4, false, true, true, SCM_AV1_MAIN8 | SCM_AV1_HIGH10_444}, + // Mode 3 never advertises 10-bit 4:4:4, whatever the probe found. + flags_case_t {codec_e::hevc, 3, true, true, true, SCM_HEVC | SCM_HEVC_MAIN10 | SCM_HEVC_REXT8_444}, + flags_case_t {codec_e::av1, 3, true, true, true, SCM_AV1_MAIN8 | SCM_AV1_MAIN10 | SCM_AV1_HIGH8_444} + ) +); diff --git a/tests/unit/test_video.cpp b/tests/unit/test_video.cpp index d03dfc40e..01046dd88 100644 --- a/tests/unit/test_video.cpp +++ b/tests/unit/test_video.cpp @@ -241,6 +241,73 @@ INSTANTIATE_TEST_SUITE_P( ) ); +/** + * @brief Reset the published probe results before and after each test. + */ +struct EncoderProbeResultsTest: testing::Test { + /** + * @brief Start from an empty published state. + */ + void SetUp() override { + video::reset_encoder_probe_results(); + } + + /** + * @brief Leave no published state behind for other tests. + */ + void TearDown() override { + video::reset_encoder_probe_results(); + } +}; + +TEST_F(EncoderProbeResultsTest, FailedCodecProbeDoesNotPublishCapabilities) { + video::encoder_t encoder {"test"sv, {}, {}, {}, {}, 0}; + for (auto *codec : {&encoder.h264, &encoder.hevc, &encoder.av1}) { + (*codec)[video::encoder_t::PASSED] = false; + (*codec)[video::encoder_t::YUV444] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE_YUV444] = true; + } + + video::record_encoder_probe_results(encoder); + + for (std::size_t i = 0; i < 3; ++i) { + EXPECT_FALSE(video::last_encoder_probe_supported_yuv444_for_codec[i]) << i; + EXPECT_FALSE(video::last_encoder_probe_supported_dynamic_range_for_codec[i]) << i; + EXPECT_FALSE(video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[i]) << i; + } +} + +TEST_F(EncoderProbeResultsTest, SubsequentProbeReplacesPriorResults) { + constexpr uint32_t ref_frames_invalidation = 1 << 8; // Mirrors REF_FRAMES_INVALIDATION in video.cpp. + video::encoder_t first {"first"sv, {}, {}, {}, {}, ref_frames_invalidation}; + for (auto *codec : {&first.h264, &first.hevc, &first.av1}) { + (*codec)[video::encoder_t::PASSED] = true; + (*codec)[video::encoder_t::YUV444] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE] = true; + (*codec)[video::encoder_t::DYNAMIC_RANGE_YUV444] = true; + } + + // The second encoder only supports plain 8-bit 4:2:0 H.264. + video::encoder_t second {"second"sv, {}, {}, {}, {}, 0}; + second.h264[video::encoder_t::PASSED] = true; + + video::record_encoder_probe_results(first); + ASSERT_TRUE(video::last_encoder_probe_supported_ref_frames_invalidation); + ASSERT_TRUE(video::last_encoder_probe_supported_yuv444_for_codec[1]); + ASSERT_TRUE(video::last_encoder_probe_supported_dynamic_range_for_codec[2]); + ASSERT_TRUE(video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[2]); + + video::record_encoder_probe_results(second); + + EXPECT_FALSE(video::last_encoder_probe_supported_ref_frames_invalidation); + for (std::size_t i = 0; i < 3; ++i) { + EXPECT_FALSE(video::last_encoder_probe_supported_yuv444_for_codec[i]) << i; + EXPECT_FALSE(video::last_encoder_probe_supported_dynamic_range_for_codec[i]) << i; + EXPECT_FALSE(video::last_encoder_probe_supported_dynamic_range_yuv444_for_codec[i]) << i; + } +} + #ifdef _WIN32 TEST(AmfH264OptionsTest, CoderUsesConfiguredValue) { const auto coder_option = std::ranges::find(video::amdvce.h264.common_options, "coder"sv, &video::encoder_t::option_t::name);