From 92c56e0de9ee737616774325ae71db1a894d369d Mon Sep 17 00:00:00 2001 From: linbo <1034003879@qq.com> Date: Tue, 25 Aug 2026 16:11:36 +0800 Subject: [PATCH] fix(microphone): resolve ALSA cards from sysfs --- .../ffmpeg_microphone/CMakeLists.txt | 1 - .../src/microphone_device_discovery.cpp | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cmvr-es/devices/microphone/ffmpeg_microphone/CMakeLists.txt b/cmvr-es/devices/microphone/ffmpeg_microphone/CMakeLists.txt index 85be85a0..b916d2f0 100644 --- a/cmvr-es/devices/microphone/ffmpeg_microphone/CMakeLists.txt +++ b/cmvr-es/devices/microphone/ffmpeg_microphone/CMakeLists.txt @@ -10,7 +10,6 @@ add_library(cmvr_es::device::ffmpeg_microphone ALIAS ffmpeg_microphone) target_link_libraries(ffmpeg_microphone PRIVATE avfilter avdevice - -lasound -lpulse cmvr_es::proto ) diff --git a/cmvr-es/devices/microphone/ffmpeg_microphone/src/microphone_device_discovery.cpp b/cmvr-es/devices/microphone/ffmpeg_microphone/src/microphone_device_discovery.cpp index 6bb9417d..e1bafb4b 100644 --- a/cmvr-es/devices/microphone/ffmpeg_microphone/src/microphone_device_discovery.cpp +++ b/cmvr-es/devices/microphone/ffmpeg_microphone/src/microphone_device_discovery.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -12,7 +13,6 @@ #include extern "C" { -#include #include #include #include @@ -53,6 +53,22 @@ std::optional parseNonNegativeInt(const char* value) return static_cast(parsed); } +std::optional findAlsaCardIndex(const std::string& card_name) +{ + constexpr int kMaxAlsaCards = 256; + const auto numeric_card_index = parseNonNegativeInt(card_name.c_str()); + for (int index = 0; index < kMaxAlsaCards; ++index) { + std::ifstream id_file( + "/sys/class/sound/card" + std::to_string(index) + "/id"); + std::string id; + if (id_file && std::getline(id_file, id) && + (id == card_name || (numeric_card_index && *numeric_card_index == index))) { + return index; + } + } + return std::nullopt; +} + std::optional parseFfmpegCaptureEndpoint(const char* device_name) { constexpr std::string_view kPlugHardwarePrefix = "plughw:"; @@ -78,12 +94,12 @@ std::optional parseFfmpegCaptureEndpoint(const char* device_name) const std::string card_name(name.substr(card_key + kCardKey.size(), card_end - card_key - kCardKey.size())); const std::string device_text(name.substr(device_key + kDeviceKey.size())); - const int card_index = snd_card_get_index(card_name.c_str()); + const auto card_index = findAlsaCardIndex(card_name); const auto device_index = parseNonNegativeInt(device_text.c_str()); - if (card_index < 0 || !device_index) { + if (!card_index || !device_index) { return std::nullopt; } - return AlsaEndpoint{card_index, *device_index}; + return AlsaEndpoint{*card_index, *device_index}; } struct PulseSourceQuery {