diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index 058d1a159..e61f30a17 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -4,10 +4,12 @@ /* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */ /* SPDX-License-Identifier: MIT */ +#include #include #include #include #include +#include #include #include @@ -739,6 +741,23 @@ uint32_t prop_id_to_control(uint32_t prop_id) return SPA_ID_INVALID; } +template +[[nodiscard]] +std::array control_info_to_range(const libcamera::ControlInfo& cinfo) +{ + static_assert(std::is_arithmetic_v); + + auto min = cinfo.min().get(); + auto max = cinfo.max().get(); + spa_assert(min <= max); + + auto def = !cinfo.def().isNone() + ? cinfo.def().get() + : (min + ((max - min) / 2)); + + return {{ min, max, def }}; +} + [[nodiscard]] spa_pod *control_details_to_pod(spa_pod_builder& b, const libcamera::ControlId& cid, const libcamera::ControlInfo& cinfo) @@ -773,14 +792,7 @@ spa_pod *control_details_to_pod(spa_pod_builder& b, spa_pod_builder_pop(&b, &f); } break; case ControlTypeFloat: { - float min = cinfo.min().get(); - float max = cinfo.max().get(); - float def; - - if (cinfo.def().isNone()) - def = (min + max) / 2; - else - def = cinfo.def().get(); + auto [ min, max, def ] = control_info_to_range(cinfo); spa_pod_builder_add(&b, SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float( @@ -788,14 +800,7 @@ spa_pod *control_details_to_pod(spa_pod_builder& b, 0); } break; case ControlTypeInteger32: { - int32_t min = cinfo.min().get(); - int32_t max = cinfo.max().get(); - int32_t def; - - if (cinfo.def().isNone()) - def = (min + max) / 2; - else - def = cinfo.def().get(); + auto [ min, max, def ] = control_info_to_range(cinfo); spa_pod_builder_add(&b, SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(